[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

async notification handling w/o threads/polling (similiar to kill -hup)?

News123

3/6/2010 11:54:00 PM

Hi,

I'd like to notify python processes asynchronously.
at notification time a callback should be called

The solution should be working on linux and Windows.

I could add a wrapper to switch between a windows / linux implementation
though

If possible I'd like to avoid
- having to poll for an external event
- having to add a thread.
- changing my existing code

I thought about using signal and os.kill()
However windows does not to have SIGHUP , SIGUSR1 or SIGUSR2.
So I'm not sure, which signal I could use with windows.


Apart from that there's one minor problem with signals
which might speak against using signal
All blocking io calls might be interrupted, which is not desirable in my
case.

Do you have any suggestions for
Linux / WIndows or both?


#### example code with signals #################
#### a blocking io call here reading a named pipe
#### would be interrupted
import signal
a = 0
def handler(signum,frame):
global a
a += 1

signal.signal(signal.SIGUSR1,handler)
print "hi"
p = open("namedpipe")
while True:
v = p.read(2)
print "V:",a,len(v)
if len(v) != 2: break

print "var a changed, but read() was interrupted :-("


bye


N
1 Answer

News123

3/7/2010 12:17:00 AM

0

News123 wrote:
> Hi,
>
> I'd like to notify python processes asynchronously.
> at notification time a callback should be called
>
> The solution should be working on linux and Windows.
>
> I could add a wrapper to switch between a windows / linux implementation
> though
>
> If possible I'd like to avoid
> - having to poll for an external event
> - having to add a thread.
Well having a blocking thread would be fine though. what I'd really want
to avoid is any polling.
> - changing my existing code
>
> I thought about using signal and os.kill()
> However windows does not to have SIGHUP , SIGUSR1 or SIGUSR2.
> So I'm not sure, which signal I could use with windows.
>
>
> Apart from that there's one minor problem with signals
> which might speak against using signal
> All blocking io calls might be interrupted, which is not desirable in my
> case.
>
> Do you have any suggestions for
> Linux / WIndows or both?
>
>
> #### example code with signals #################
> #### a blocking io call here reading a named pipe
> #### would be interrupted
> import signal
> a = 0
> def handler(signum,frame):
> global a
> a += 1
>
> signal.signal(signal.SIGUSR1,handler)
> print "hi"
> p = open("namedpipe")
> while True:
> v = p.read(2)
> print "V:",a,len(v)
> if len(v) != 2: break
>
> print "var a changed, but read() was interrupted :-("
>
>
> bye
>
>
> N