Vincent
2008-Feb-13 12:46 UTC
[asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't work
Hello When a call comes in, I'd like to fork a Python script that broadcasts a message so that users see the CID name + number pop up on their computer screen, and simultaneously ring their phones. The following script doesn't work as planned: It waits until the script ends before moving on to the next step, which is Dial(): ==========exten => s,1,AGI(netcid.py|${CALLERID(num)}|${CALLERID(name)}) exten => s,n,Dial(${MYPHONE},5) ==========# cat netcid.py #!/usr/bin/python import socket,sys,time,os def sendstuff(data): s.sendto(data,(ipaddr,portnum)) return sys.stdout = open(os.devnull, 'w') if os.fork(): #BAD? sys.exit(0) os._exit(0) else: now = time.localtime(time.time()) dateandtime = time.strftime("NaVm/%y NaVM", now) myarray = [] myarray.append("STAT Rings: 1") myarray.append("RING") myarray.append("NAME " + cidname) myarray.append("TTSN Call from " + cidname) myarray.append("NMBR " + cidnum) myarray.append("TYPE K") s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,True) portnum = 42685 ipaddr = "192.168.0.255" for i in myarray: sendstuff(i) #Must pause, and send IDLE for dialog box to close time.sleep(5) sendstuff("IDLE " + dateandtime) ========== In another forum, people told me that I should fork twice. Is that really necessary? http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731 Thank you.
Diego Aguirre
2008-Feb-13 12:59 UTC
[asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't work
Vincent, try to use System() instead of AGI() Diego Aguirre Infodag - Inform?tica FWD#: 459696 Nikotel#: 99 21 8138-2710 EnumLookup#: +55 21 8138-2710 DUNDi-br#: 21 8138-2710 Vincent escreveu:> Hello > > When a call comes in, I'd like to fork a Python script that > broadcasts a message so that users see the CID name + number pop up on > their computer screen, and simultaneously ring their phones. > > The following script doesn't work as planned: It waits until the > script ends before moving on to the next step, which is Dial(): > > ==========> exten => s,1,AGI(netcid.py|${CALLERID(num)}|${CALLERID(name)}) exten > => s,n,Dial(${MYPHONE},5) > ==========> # cat netcid.py > #!/usr/bin/python > > import socket,sys,time,os > > def sendstuff(data): > s.sendto(data,(ipaddr,portnum)) > return > > sys.stdout = open(os.devnull, 'w') > if os.fork(): > #BAD? sys.exit(0) > os._exit(0) > else: > now = time.localtime(time.time()) > dateandtime = time.strftime("NaVm/%y NaVM", now) > > myarray = [] > myarray.append("STAT Rings: 1") > myarray.append("RING") > myarray.append("NAME " + cidname) > myarray.append("TTSN Call from " + cidname) > myarray.append("NMBR " + cidnum) > myarray.append("TYPE K") > > s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) > s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,True) > > portnum = 42685 > ipaddr = "192.168.0.255" > > for i in myarray: > sendstuff(i) > > #Must pause, and send IDLE for dialog box to close > time.sleep(5) > sendstuff("IDLE " + dateandtime) > ==========> > In another forum, people told me that I should fork twice. Is that > really necessary? > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731 > > Thank you. > > > _______________________________________________ > -- Bandwidth and Colocation Provided by http://www.api-digital.com -- > > asterisk-users mailing list > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users >
Michiel van Baak
2008-Feb-13 13:25 UTC
[asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't work
On 13:46, Wed 13 Feb 08, Vincent wrote:> Hello > > When a call comes in, I'd like to fork a Python script that > broadcasts a message so that users see the CID name + number pop up on > their computer screen, and simultaneously ring their phones. > > The following script doesn't work as planned: It waits until the > script ends before moving on to the next step, which is Dial(): > > In another forum, people told me that I should fork twice. Is that > really necessary? > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731If you want it to detach the program from it's parent you need the double fork yes. -- Michiel van Baak michiel at vanbaak.eu http://michiel.vanbaak.eu GnuPG key: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x71C946BD "Why is it drug addicts and computer aficionados are both called users?"