I normally starts the thread using the following command,
threading.Thread(target=function_name).start()
Is there any way to pause it like
threading.Thread(target=name).wait()
and resume is like
threading.Thread(target=name).set()
I normally starts the thread using the following command,
threading.Thread(target=function_name).start()
Is there any way to pause it like
threading.Thread(target=name).wait()
and resume is like
threading.Thread(target=name).set()
How about check this project. This project meets all your requirements. Python's built-in threading module does not support play and pause functionality for now.
So to install that modules: pip install pythreadworker
After installation here is a example:
from worker import Worker # We import the Worker to create thread
def some_long_running_task(foo, bar, tee="foo"):
do_random_long_stuff()
# ...
if __name__ == '__main__':
# to make a thread we initialise Worker or somewhat so called the Thread of threading module
thread = Worker(some_long_running_task) # remember just pass the function without execution parenthesis
# Now to start thread while also supplying arguments, we call start method
thread.start("bar", bar="foo", tee=123)
# To pause a the thread we use pause method
thread.pause()
# To resume it we use resume method
thread.resume()
# To use the Thread.join Worker class, use the join method
thread.join()
# And to kill it we use
thread.stop()
But do remember while using that if you are using time.sleep
then you will have to replace it with worker.sleep
so for example you import code is
from time import sleep
replace it with
from worker import sleep
Hope this helps.
The threading module has a Semaphore, which might help. If there isn't something already built-in to the threading module, you could try using a Tkinter semaphore variable specific to each thread, and have the thread check its semaphore while in some polling loop.