如何在不使用join的情况下向python中的主线程发送信号? [英] How to send a signal to the main thread in python without using join?

查看:256
本文介绍了如何在不使用join的情况下向python中的主线程发送信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从子线程向多线程程序中的主线程发送信号(不能使用多进程).不幸的是,即使用尽了所有在线阅读材料(我可以找到),我也仍然不清楚如何做到这一点.我是发信号给AND和python的初学者,所以请忍受我,向新手解释. 我不能在该过程中使用join方法,因为我希望两个线程同时运行. 这是我在此处找到的与该主题相关的代码- http://pymotw.com /2/signal/#signals-and-threads 对我来说真的不起作用.

I Am trying to send a signal from a child thread to the main thread in a multi-threaded program (cannot use multi-processes). Unfortunately even after exhausting all the reading materials available online (which I could find), I Am unable to get a clear idea of how to do so. I Am a beginner to signals AND to python so please bear with me and explain as you would to a novice. I cannot use the join method in the process, since I want both the threads to be running simultaneously. Here is the code that I found related to the topic here - http://pymotw.com/2/signal/#signals-and-threads and it doesn't really work for me.

import signal
import threading
import os
import time

def signal_handler(num, stack):
    print 'Received signal %d in %s' % (num, threading.currentThread())

signal.signal(signal.SIGUSR1, signal_handler)

def wait_for_signal():
    print 'Waiting for signal in', threading.currentThread()
    signal.pause()
    print 'Done waiting'

# Start a thread that will not receive the signal
receiver = threading.Thread(target=wait_for_signal, name='receiver')
receiver.start()
time.sleep(0.1)

def send_signal():
    print 'Sending signal in', threading.currentThread()
    os.kill(os.getpid(), signal.SIGUSR1)

sender = threading.Thread(target=send_signal, name='sender')
sender.start()
sender.join()

# Wait for the thread to see the signal (not going to happen!)
print 'Waiting for', receiver
signal.alarm(2)
receiver.join()

如果可能,请使用多线程示例进行说明. 预先感谢!

Please explain with a multi-threaded example if possible. Thanks in advance!

推荐答案

信号和线程真的,真的不能很好地发挥作用.

Signals and threads really, really don't play nice together.

请考虑使用Event或其他同步机制.下面的示例创建一个事件"对象,然后将其传递给两个线程.一个等待两秒钟,然后发信号通知另一个打印出一条消息,然后退出.

Consider use an Event or other synchronization mechanism. The following example creates an 'event' object, then passes it to two threads. One waits for two seconds, then signals the other to print out a message then exit.

import threading, time

def flagger_thread(event):
    """
    wait for two seconds, then make 'event' fire
    """
    time.sleep(2)
    event.set()

def waiter_thread(event):
    print("Waiting for event")
    if event.wait(5):
        print("event set.")
    else:
        print("Timed out.")

stop_event = threading.Event()
threading.Thread(target=flagger_thread, args=[stop_event]).start()
threading.Thread(target=waiter_thread, args=[stop_event]).start()

# wait for all threads to exit
for t in threading.enumerate():
    if t != threading.current_thread():
        t.join()

输出

Waiting for event
event set.

这篇关于如何在不使用join的情况下向python中的主线程发送信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆