[asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't work

Vincent vincent.delporte at bigfoot.com
Wed Feb 13 06:46:04 CST 2008


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.




More information about the asterisk-users mailing list