python会在线程中有信号处理程序吗? [英] Will python ever have signalhandlers in threads?

查看:58
本文介绍了python会在线程中有信号处理程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过信号模块和示例

并得出结论,示例不会工作

如果您尝试在线程中执行此操作。


那么类似的代码是否有机会在一个帖子中运行?


-

Antoon Pardon

解决方案

Op 2004-11-09,Antoon Pardon schreef< ap ***** @ forel.vub.ac.be>:< blockquote class =post_quotes>我已经看过信号模块和示例
并得出结论,如果您尝试在一个线程中执行此操作,该示例将无法工作。

那么类似的代码是否有可能在一个线程中起作用?



我想,缺乏响应意味着anser是:

不太可能。

可惜!


-

Antoon Pardon


Antoon Pardon写道:

Op 2004-11-09,Antoon Pardon schreef< ap ***** @ forel。 vub.ac.be>:

我看了一下信号模块和示例
并得出结论,如果你试图这个例子不会工作
这是一个线程。

那么类似的代码是否有可能在一个线程中工作?



我想,缺乏响应意味着anser是:

不太可能。




很抱歉,我没有时间浏览你提到的代码,但是最近

for ipython我不得不处理线程中的信号处理。如果你关心的话,你可以查看

IPython'的Shell.py来解决我最终使用的黑客攻击。


它的主旨是这个功能,安装到gtk / WX计时器:


def runco​​de(self):

"""执行代码对象。 />

围绕IPython'的runco​​de()的多线程包装。"""


#锁定线程保护的东西

self.ready.acquire()


#安装sigint处理程序

signal.signal(signal.SIGINT,sigint_handler)


if self._kill:

print>> Term.cout,''关闭线程......'',

Term.cout。 flush()

forkill in self.on_kill:

tokill()

print>> Term.cout,''完成。 ''


#通过调用父类运行挂起代码

如果self.code_to_run不是None:

self.ready。 notify()

self.parent_runco​​de(self.code_to_run)

#清除c已运行的ode对象

self.code_to_run =无


#我们已完成线程保护变量

self.ready.release()

#必须返回true才能运行gtk线程

返回True


如你所见在锁定后,我最终在*每次*调用

函数时安装了处理程序。我无法找到如何将处理程序分配给线程的
,所以这是我的暴力方法。


这非常难看,而且可能有更好的解决方案。但是在这个具体的案例中,黑客的工作和最终用户的惩罚并不明显。


最好,


f


>我已经看过信号模块和示例

并得出结论,如果你尝试在一个线程中执行此操作,示例将无法工作。




我认为根据Posix Thread规范,信号只被删除到

这个过程,永远不会线程......我可能错了......


-

damjan


I have had a look at the signal module and the example
and came to the conclusion that the example wont work
if you try to do this in a thread.

So is there a chance similar code will work in a thread?

--
Antoon Pardon

解决方案

Op 2004-11-09, Antoon Pardon schreef <ap*****@forel.vub.ac.be>:

I have had a look at the signal module and the example
and came to the conclusion that the example wont work
if you try to do this in a thread.

So is there a chance similar code will work in a thread?


Well I guess that the lack of response means the anser is:

Not likely.
Pity!

--
Antoon Pardon


Antoon Pardon wrote:

Op 2004-11-09, Antoon Pardon schreef <ap*****@forel.vub.ac.be>:

I have had a look at the signal module and the example
and came to the conclusion that the example wont work
if you try to do this in a thread.

So is there a chance similar code will work in a thread?


Well I guess that the lack of response means the anser is:

Not likely.



Sorry that I don''t have time to go through the code you mention, but recently
for ipython I had to deal with signal handling in threads. You can look in
IPython''s Shell.py for the hacks I ended up using, if you care.

The gist of it is this function, which gets installed into a gtk/WX timer:

def runcode(self):
"""Execute a code object.

Multithreaded wrapper around IPython''s runcode()."""

# lock thread-protected stuff
self.ready.acquire()

# Install sigint handler
signal.signal(signal.SIGINT, sigint_handler)

if self._kill:
print >>Term.cout, ''Closing threads...'',
Term.cout.flush()
for tokill in self.on_kill:
tokill()
print >>Term.cout, ''Done.''

# Run pending code by calling parent class
if self.code_to_run is not None:
self.ready.notify()
self.parent_runcode(self.code_to_run)
# Flush out code object which has been run
self.code_to_run = None

# We''re done with thread-protected variables
self.ready.release()
# This MUST return true for gtk threading to work
return True

As you see, I ended up installing the handler on *every* invocation of the
function, after taking a lock. I couldn''t find how to assign the handler to
the thread, so this was my brute-force approach.

This is incredibly ugly, and there may be a better solution. But in this
particular case, the hack works and the penalty is not noticeable to end users.

Best,

f


> I have had a look at the signal module and the example

and came to the conclusion that the example wont work
if you try to do this in a thread.

So is there a chance similar code will work in a thread?



I think acording to the Posix Thread specs, signals are only delevered to
the process and never to threads... I could be wrong though...

--
damjan


这篇关于python会在线程中有信号处理程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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