[英]Python raise exception by dbus signal with multithreading concept
我有一个特定的问题。
例如:我收到信号,这意味着我必须处理数字。 线程池中的任何线程都将从队列中取出该项目并启动处理数字上某些内容的功能-这可能需要很长时间。 因此,在任何时间之后,我都会收到有关当前状态的信号,并且需要发送检测的当前状态-这就是为什么我使用线程(用于共享内存)。 我还可以从D-Bus接收中断信号(“时间太长,请停止此检测,并可以接受其他请求”)。 而中断是主要问题...
所以我的主要问题是:
threads_init()
) 程序:
SERVICE = multiprocessing.Process(target=dbus_signal_receiver, args=(...))
SERVICE.daemon = True
SERVICE.start()
class worker(threading.Thread):
def __init__(self,...):
threading.Thread.__init__(self)
def run(self):
while True:
#get item from queue
s = threading.Thread(target=curr_and_interr_signal_handle, args=(ID of item from queue,...))
s.daemon = True
s.start()
#start specific detection based on request
for i in range(number of threads):
t = worker(...)
t.daemon = True
t.start()
我希望这样的事情可以工作...(但是不行)
...
class worker(threading.Thread):
def __init__(self,...):
threading.Thread.__init__(self)
def run(self):
while True:
try:
#get item from queue
s = threading.Thread(target=curr_and_interr_signal_handle, args=(ID of item from queue,...))
s.daemon = True
s.start()
#start specific detection based on request
except raised_interrupt_exception:
#continue - wait for another request from queue
...
阅读有关18.8.1.2。 信号和线程
即使在另一个线程中接收到信号,Python信号处理程序也总是在主Python线程中执行。
这意味着信号不能用作线程间通信的手段。
您可以改用来自线程模块的同步原语。
此外,仅允许主线程设置新的信号处理程序。
尚不清楚为什么必须在thread in thread
使用thread in thread
。
为什么您的worker thread
无法处理检测?
例如,应该执行以下操作:
def run(self):
while self.running.is_set():
#get item from queue
#start specific detection based on request
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.