多线程与多处理

发布于 2025-01-01 13:16:09 字数 534 浏览 1 评论 0原文

我目前有一个按钮触发此操作:

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 技术交流群。

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

发布评论

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

评论(2

涙—继续流 2025-01-08 13:16:09

为什么不替换“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()").

ゃ懵逼小萝莉 2025-01-08 13:16:09

你可以尝试这样的事情,我自己也做过。

from Queue import Queue
import threading

class workingthread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self._queue = Queue()

    def run(self):
        while True:

            item = self._queue.get(block=True)
            if item is None:
                return

            name, message = item
            chat = skype.CreateChatWith(name)
            chat.SendMessage(message)

    def processOne(self, data):
        self._queue.put_nowait(data)

    def stop(self):
        self._queue.put(None)

显然,这是一个粗略的示例,但是您创建了一个在线程中阻塞的队列。您可以随时启动该线程,它就会运行。它等待您触发它或停止它。

You could try something like this, which I have done myself.

from Queue import Queue
import threading

class workingthread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self._queue = Queue()

    def run(self):
        while True:

            item = self._queue.get(block=True)
            if item is None:
                return

            name, message = item
            chat = skype.CreateChatWith(name)
            chat.SendMessage(message)

    def processOne(self, data):
        self._queue.put_nowait(data)

    def stop(self):
        self._queue.put(None)

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.

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