Python 切换线程
我应该继续使用这样的线程,还是应该使用多处理?我正在尝试通过按下按钮来切换while循环 线程:Python 切换线程,python,multithreading,wxpython,Python,Multithreading,Wxpython,我应该继续使用这样的线程,还是应该使用多处理?我正在尝试通过按下按钮来切换while循环 线程: class workingthread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): while 1: chat = skype.CreateChatWith(name) chat.
class workingthread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while 1:
chat = skype.CreateChatWith(name)
chat.SendMessage(message)
t.sleep(timeou)
启动线程:
def this(self,event):
t = workingthread()
t.start()
你的问题有点不清楚。我试着把注意力集中在我试着让while循环通过按钮按下部分来切换。根据我的理解,您希望始终执行线程活动,而我认为不会终止线程,但您只希望偶尔执行while循环体。最简单的解决方案是实现一个布尔变量并将其扩展如下:
class workingthread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.doit = True
def run(self):
while 1:
if doit:
# if the execution of both statements is desired
chat = skype.CreateChatWith(name)
chat.SendMessage(message)
# sleep in both cases
t.sleep(timeou)
现在,您可以使用一个按钮事件,不管您使用的是什么UI工具包,将一个方法绑定到它,并可以切换WorkingRead的doit变量。while循环主体是否执行取决于doit。我认为您没有提供足够的解释供任何人帮助。我将编辑我的帖子并添加更多信息。