Reputation: 1088
[Challenge]:
I have two threads, the first one is some_signal and the second one is post_proccesing.
For the first iteration, I would like to run, post_proccesing after some_signal.
And after the first iteration. I would like to start post_proccesing thread, so post_proccesing would use some_signal data from the previous loop.
[Pseudocode]:
Second iteration:
start treading
some_signal and post_proccesing[some_signal-1]
[My Attempt]:
I have tried to implement it using the following way, but I'm not entirely sure,if I've done it correctly:
import threading
def some_signal():
print threading.currentThread().getName(), 'Get signal'
def post_proccesing():
print threading.currentThread().getName(), 'Process the signa;'
t = threading.Thread(name='post_proccesing', target=post_proccesing)
w = threading.Thread(name='some_signal', target=some_signal)
flag = 0;
for i in range(5):
t = threading.Thread(target=some_signal) # use default name
if flag == 0:
some_signal() # use default name
flag = flag + 1;
else:
w = threading.Thread(target=post_proccesing) # use default name
w.start()
t.start()
Upvotes: 1
Views: 42
Reputation: 5774
It sounds to me like you can implement logic based on i
(your iteration count). Maybe something like this would suit you (I'm not sure what intent you have with flag
so I removed it):
import threading
def some_signal():
print(threading.currentThread().getName(), 'Get signal')
def post_proccesing():
print(threading.currentThread().getName(), 'Process the signa;')
for i in range(5):
if i: # means i > 0 because 0 -> False
t = threading.Thread(target=some_signal) # use default name
# removed 'else' statement because you need to have a 'w' variable for your call to 'w.start()'
w = threading.Thread(target=post_proccesing) # use default name
t.start()
w.start()
else: # case where i == 0 -> first iteration
some_signal()
post_proccesing()
Output:
MainThread Get signal
MainThread Process the signa;
Thread-1 Get signal
Thread-2 Process the signa;
Thread-3 Get signal
Thread-4 Process the signa;
Thread-5 Get signal
Thread-6 Process the signa;
Thread-7 Get signal
Thread-8 Process the signa;
Upvotes: 1