'How to start python thread with control?
I am developing my school project and it requires starting threads with control. The following code is a sample that simulates my scenario.
GrandFather has 3 children [Mike, William, John] these children are fathers now. so they also have children
"Mike": ["Mathew", "Miller"]
"William": ["Willy", "Winson", "Walt"]
"John": ["Jude"]
Father and Son classes are thread classes. GrandFather create Father classes according to their age (order). Then Father create his Sons classes
but the Requirement is Grandfather should wait until the elder son creates his next sons. (Father can create their sons without any order )
Example of execution.
- Grandfather creates Mike
- Mike creates Mathew and Miller
- Grandfather creates William
- William creates Willy, Winson and Walt
- Grandfather creates John
- John creates Jude
So I have written the following code and it is not working as expected.
import threading
import time
from typing import List
class GrandFather:
def __init__(self):
self.sons_of_granfather = {1: "Mike", 3: "John", 2: "William"}
self.sons_of_father = {"Mike": ["Mathew", "Miller"], "William": ["Willy", "Winson", "Walt"], "John": ["Jude"]}
self.created_next_son = True
def run(self):
print(f"{threading.current_thread()}_run()\n")
sons_in_order = list(self.sons_of_granfather.keys())
for order in sorted(sons_in_order):
name = self.sons_of_granfather.get(order)
children_of_father = self.sons_of_father.get(name)
father = Father(name, children_of_father, self.created_next_son)
self.created_next_son = False
father.start()
while not self.created_next_son:
print(self.created_next_son)
time.sleep(1)
class Father(threading.Thread):
def __init__(self, name: str, children: List[str], created_next_son: bool):
super().__init__()
self.name = name
self.children = children
self.created_next_son = created_next_son
self.setName(self.name)
self.daemon = True
def run(self):
print(f"{threading.current_thread()}_run()\n")
for childName in self.children:
son = Son(self.name, childName)
son.start()
self.created_next_son = True
class Son(threading.Thread):
def __init__(self, fatherName, childName):
super().__init__()
self.fatherName = fatherName
self.childName = childName
self.setName(f"{self.fatherName}_{self.childName}")
self.daemon = True
def run(self):
print(f"{threading.current_thread()}_run()\n")
grandFather = GrandFather()
grandFather.run()
I am maintaining a variable to identify when to create a father. but it gives the following output and won't continue because self.created_next_son becomes True again
<_MainThread(MainThread, started 15276)>_run()
<Father(Mike, started daemon 12536)>_run()
False
<Son(Mike_Mathew, started daemon 10016)>_run()
<Son(Mike_Miller, started daemon 5824)>_run()
It remains in the while loop forever. please help me to solve this issue
Solution 1:[1]
Solved my problem using threading.Event
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AusCango |
