切换线程

2024-04-26 00:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我应该继续使用这样的线程还是应该使用多处理?我试着让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()

Tags: runnameselfinitdefchat线程按钮
1条回答
网友
1楼 · 发布于 2024-04-26 00:23:25

你的问题有点不清楚。我试着把注意力集中在“我试着让while循环通过按下按钮来切换”部分。据我所知,您希望一直有线程活动(通过活动我认为不会终止线程),但您只希望偶尔执行while循环体。最简单的解决方案是实现一个布尔变量并像这样扩展workingthread

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工具箱),将方法绑定到它,并且可以切换workingthreaddoit-变量。根据doit,while循环体是否执行。你知道吗

相关问题 更多 >