How to listen event with Python threading
Question:
I’m trying to implement a multi thread program in python and am having troubles.
I try to design a program, when the program (main thread) receives a specific command,
The counter of the program will return to the previous number and continue counting down.
The following is the code I tried to write:
import threading
import time
count = 0
preEvent = threading.Event()
runEvent = threading.Event()
runEvent.set()
preEvent.clear()
def pre():
global count
while True:
if event.isSet():
count -= 1
event.clear()
def doSomething():
global count
while True:
# if not runEvent.isSet():
# runEvent.wait()
print(count)
count += 1
time.sleep(1)
def main():
t1 = threading.Thread(target=pre, args=())
t2 = threading.Thread(target=doSomething, args=())
t1.start()
t2.start()
command = input("input command")
while command != 'quit':
command = input("input command")
if command == 'pre':
preEvent.set()
main()
But I encountered a few problems
-
How to block t1 simultaneously while I inputting a specific command
-
How to start from the beginning when t1 is restored instead of starting from the blocked point
Regarding question 1, I tried adding a condition check before the print(count) command, but if I enter the "pre" command when the program outputs count, the program will still perform count+=1
After that, the program will go back to check the conditions and block t1, but this does not achieve the synchronization effect I wanted.
Is there a way to achieve this goal?
Question 2 is similar to question 1. I hope that whenever I enter the command, my program will output like this.
1
2
3
4
pre
3
4
...
But if t1 is blocked after finishing the print(count) instruction, when the event is cleared, t1 will continue to execute from count+=1
So the output will become the following
1
2
3
4
pre
4
5
...
I tried to find information on the Internet, but I never knew how to add keywords.
Is there a method or library that can achieve this function?
I have tried my best to describe my problem, but it may not be good enough. If I have any questions about my problem, I can add more explanation.
Thank you all for your patience to read my question
Answers:
The threading module has a wait method that will block until notify or notify_all is called. This should accomplish what you’re looking for in your first question. For question 2 you can either define a function that handles the exit case, or just recreate a thread to start form the beginning.
Simply it can work like this, maybe helps.
from threading import Thread, Event
count = 0
counter = Event()
def pre():
global count
counter.wait()
count -= 1
print(count)
def main():
global count
print(count)
while True:
command = input("Enter 1 to increase, Enter 2 to decrease :")
if command == "1":
t1 = Thread(target=pre, args=())
t1.start()
counter.set()
else :
count += 1
print(count)
main()
I’m trying to implement a multi thread program in python and am having troubles.
I try to design a program, when the program (main thread) receives a specific command,
The counter of the program will return to the previous number and continue counting down.
The following is the code I tried to write:
import threading
import time
count = 0
preEvent = threading.Event()
runEvent = threading.Event()
runEvent.set()
preEvent.clear()
def pre():
global count
while True:
if event.isSet():
count -= 1
event.clear()
def doSomething():
global count
while True:
# if not runEvent.isSet():
# runEvent.wait()
print(count)
count += 1
time.sleep(1)
def main():
t1 = threading.Thread(target=pre, args=())
t2 = threading.Thread(target=doSomething, args=())
t1.start()
t2.start()
command = input("input command")
while command != 'quit':
command = input("input command")
if command == 'pre':
preEvent.set()
main()
But I encountered a few problems
-
How to block t1 simultaneously while I inputting a specific command
-
How to start from the beginning when t1 is restored instead of starting from the blocked point
Regarding question 1, I tried adding a condition check before the print(count) command, but if I enter the "pre" command when the program outputs count, the program will still perform count+=1
After that, the program will go back to check the conditions and block t1, but this does not achieve the synchronization effect I wanted.
Is there a way to achieve this goal?
Question 2 is similar to question 1. I hope that whenever I enter the command, my program will output like this.
1
2
3
4
pre
3
4
...
But if t1 is blocked after finishing the print(count) instruction, when the event is cleared, t1 will continue to execute from count+=1
So the output will become the following
1
2
3
4
pre
4
5
...
I tried to find information on the Internet, but I never knew how to add keywords.
Is there a method or library that can achieve this function?
I have tried my best to describe my problem, but it may not be good enough. If I have any questions about my problem, I can add more explanation.
Thank you all for your patience to read my question
The threading module has a wait method that will block until notify or notify_all is called. This should accomplish what you’re looking for in your first question. For question 2 you can either define a function that handles the exit case, or just recreate a thread to start form the beginning.
Simply it can work like this, maybe helps.
from threading import Thread, Event
count = 0
counter = Event()
def pre():
global count
counter.wait()
count -= 1
print(count)
def main():
global count
print(count)
while True:
command = input("Enter 1 to increase, Enter 2 to decrease :")
if command == "1":
t1 = Thread(target=pre, args=())
t1.start()
counter.set()
else :
count += 1
print(count)
main()