开发者

How to signal yourself (same PID)?

OK, so this may be an odd situation but please bear with me.

I have a Python program which calls up a C++ class via a SWIG interface. I came to a point where I must asynchronously signal (to update a status) the Python code from the C++ library. Originally I had inefficient busy loops which polled a flag. I wanted to replace this by using SIGUSR1.

So, the problem is I discovered that even though these are separate 'threads', they share the same PID. That is the Python and C++ program both report the same PID. I have sent the signal using kill, and the Python code caught it in its handler, however it did not interrupt the code as I expected开发者_如何转开发. I tried two methods of waiting for the signal in the Python code, first py calling Python's signal.pause which I read was supposed to be preempted on the reception of a signal. The other one was a simple time.sleep which was supposed to do basically the same thing - return when the signal comes through.

For some reason this isn't working - my C++ code sends the signal, the Python code receives it and calls the handler, however, the pause/sleep calls never return.

If it is possible to correctly signal the same process, how would you do it?

(and if this is just dumb forgive me and move on)


Signals are not the right tool for the job here. Normally, this would be a job for inter-thread synchronization primitives, such as

  • the locks from the thread module
  • the Event objects from the threading module

However, it's not easy to manipulate Python thread locks from C++. So I would use the old-fashioned, but very simple, approach of

  • a pipe, which the Python thread reads from, and the C++ thread writes exactly one byte to when it wants to wake up the Python.


If you are in the same program I am not sure what you would need signals for in this case. Take a look at the observer pattern. If you have your python event handlers subscribe to events in your C++ library, you can avoid signals all together.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜