切换线程

发布于 2024-12-29 04:39:26 字数 440 浏览 2 评论 0原文

我应该继续使用这样的线程还是应该使用多处理?我正在尝试通过按下按钮来切换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-01-05 04:39:26

你的问题有点不清楚。我试图专注于“我试图让 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 工具包),将方法绑定到它,并且可以切换 >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:

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)

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 the workingthread. Depending on doit, the while loop body is executed or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文