我对Python中的多线程很新,所以我还没有那么远.
除了让调用线程等待,如果在我的线程正在执行某些操作时调用暂停,我几乎可以做的事情.
以下是我在代码中尝试实现的概述:
import threading, time
class Me(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
#flag to pause thread
self.paused = False
def run(self):
while True:
if not self.paused:
#thread should do the thing if
#not paused
print 'do the thing'
time.sleep(5)
def pause(self):
self.paused = True
#this is should make the calling thread wait if pause() is
#called while the thread is 'doing the thing', until it is
#finished 'doing the thing'
#should just resume the thread
def resume(self):
self.paused = False
我想我基本上需要一个锁定机制,但在同一个线程内?
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/python-wokeyizantinghehuifudexiancheng.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 27, 2019 at 12:47 下午