切换线程
我应该继续使用这样的线程还是应该使用多处理?我正在尝试通过按下按钮来切换 while 循环。
线程:
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()
Should i keep using threads like this or should i use multiprocessing? I'm trying to get the while loop to toggle with a button press.
Thread:
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)
Start the thread:
def this(self,event):
t = workingthread()
t.start()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题有点不清楚。我试图专注于“我试图让 while 循环通过按下按钮进行切换”部分。据我了解,您希望始终有线程活动(通过活动,我认为不杀死线程),但您只想偶尔执行 while 循环体。最简单的解决方案是实现一个布尔变量并扩展
workingthread
,如下所示:现在您可以使用按钮事件(无论您使用什么 UI 工具包),将方法绑定到它,并且可以切换
>doit
-工作线程
的变量。 while 循环体是否执行取决于doit
。Your question is a little bit unclear. I'm trying to focus on the "I'm trying to get the while loop to toggle with a button press" part. To my understanding, you want to have thread activity all the time (by activity I think of not killing the thread), but you only want to occasionally have while-loop body executed. The simplest solution is to implement a boolean variable and extending
workingthread
like this:Now you can use a button event (whatever UI toolkit you are using), bind a method to it, and can toggle the
doit
-variable of theworkingthread
. Depending ondoit
, the while loop body is executed or not.