python 中如何退出线程

发布于 2025-01-12 06:59:18 字数 653 浏览 0 评论 0原文

大家好,我正在用 python 编写套接字编程并使用多线程,但是当我想退出程序时遇到一个问题,似乎我无法退出正在运行的线程。

我的代码图片

def create_workers():
for _ in range(NUMBER_OF_THREADS):
    t = threading.Thread(target=work)
    t.daemon = True  # End the Thread
    t.start()



def work():
    while True:

        x = queue.get()
        if x == 1:
            create_socket()
            bind_socket()
            accept_connections()
        if x == 2:
            start_turtle()
            break

        queue.task_done()

函数create_workers正在运行两个线程并且目标函数工作,但我不这样做在函数工作中打破 while 循环后真的知道终止它

Hi guy I am writing a socket programming in python and using multithreading but I have one problem when I want to exit a program It seem like I can not exit a running thread.

picture of my code

def create_workers():
for _ in range(NUMBER_OF_THREADS):
    t = threading.Thread(target=work)
    t.daemon = True  # End the Thread
    t.start()



def work():
    while True:

        x = queue.get()
        if x == 1:
            create_socket()
            bind_socket()
            accept_connections()
        if x == 2:
            start_turtle()
            break

        queue.task_done()

the function create_workers are running two thread and targeting function work but I don't really know to terminate it after I break a while loop in function work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

荒人说梦 2025-01-19 06:59:19

使用 threading.Event实例作为您在 work 结束之前设置的标志,并检查它是否在无限循环的每次迭代开始时设置。

如果您的 work 函数更复杂,有多个 return 语句,您可以将 event.set() 调用放入 finally 中try 语句的 块。

threading.Event 是线程安全的。

正如user2357112 支持 Monica 所指出的,使线程成为守护进程没有意义,所以我删除了该行。

def create_workers():
    event = threading.Event()
    for _ in range(NUMBER_OF_THREADS):
        t = threading.Thread(target=work, args=(event,))
        t.start()

def work(event):
    while True:
        if event.is_set():
            return
        
        x = queue.get()
        if x == 1:
            create_socket()
            bind_socket()
            accept_connections()
        if x == 2:
            start_turtle()
            break
        
        queue.task_done()
    
    event.set()

Use a threading.Event instance as a flag that you set just before work ends, and check if it is set at the start of each iteration of the infinite loop.

If your work function is more complicated, with multiple return statements, you could chuck the event.set() call into the finally block of a try statement.

threading.Event is thread-safe.

As pointed out by user2357112 supports Monica, making the threads daemonic doesn't make sense, so I've removed that line.

def create_workers():
    event = threading.Event()
    for _ in range(NUMBER_OF_THREADS):
        t = threading.Thread(target=work, args=(event,))
        t.start()

def work(event):
    while True:
        if event.is_set():
            return
        
        x = queue.get()
        if x == 1:
            create_socket()
            bind_socket()
            accept_connections()
        if x == 2:
            start_turtle()
            break
        
        queue.task_done()
    
    event.set()
扭转时空 2025-01-19 06:59:19

您可以使用python-worker链接

from worker import abort_all_thread

## your running code

abort_all_thread() # this will kill any active threads

you can use python-worker (link)

from worker import abort_all_thread

## your running code

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