多线程与多处理
我目前有一个按钮触发此操作:
def spam(self,event):
t = workingthread()
t.start()
哪个是这样的:
class workingthread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while 1:
chat = skype.CreateChatWith(name)
chat.SendMessage(message)
time.sleep(timeou)
我正在尝试切换此操作,但我听说没有办法停止线程,而且我真的不想使用多处理。我还有其他方法可以让它在不落后 wx gui 的情况下工作吗?
I currently have a button triggering this:
def spam(self,event):
t = workingthread()
t.start()
Which goes to this:
class workingthread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while 1:
chat = skype.CreateChatWith(name)
chat.SendMessage(message)
time.sleep(timeou)
I'm trying to have this be toggled but I've heard there's no way to stop threads and I don't really want to use multiprocessing. Any other ways i could get this to work without lagging the wx gui?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不替换“while 1”,而是替换一些以“true”开头的变量,并且您可以更改为“false”(指示线程退出)。
当然,这是假设您不断循环(并且没有在“chat.*”方法中阻塞,或者在“sleep()”中等待)。
Instead of "while 1", why not substitute some variable that starts "true", and you can change to "false" (signalling the thread to exit).
This assumes, of course, that you're continually looping (and not blocked in a "chat.*" method, or waiting in a "sleep()").
你可以尝试这样的事情,我自己也做过。
显然,这是一个粗略的示例,但是您创建了一个在线程中阻塞的队列。您可以随时启动该线程,它就会运行。它等待您触发它或停止它。
You could try something like this, which I have done myself.
This is a rough example obviously, but you create a queue that blocks in the thread. You start the thread whenever you want and it runs. It waits for you to trigger it or stop it.