python multhreading…

Hello,

Python multithreating module is very easy to use. I have created this almost unuseful script to generate with an asterisk equipment calls to every 10 seconds.

For this you have to know that to generate calls with asterisk, you can send 1.call files (x.call) in the /var/spool/asterisk/outgoing directory .

Here is a sample of 1.call file:

Channel: SIP/me

MaxRetries: 1

RetryTime: 60

WaitTime: 30

Context: me

Extension: you

Priority: 1

where me and you are declared channel in sip.conf

I have created the following script that sends every 10 sec the files into the good directory (first process) and notify the second process that a call has been generated.


#!/usr/bin/env python

import thread
import time
from threading import *
import subprocess

class call_gen(Thread):

def __init__(self,condition,sleeptime=10):
    Thread.__init__(self)
    self.cond=condition
    self.sleeptime=sleeptime

def run(self):
    cond=self.cond

    while 1 :
        cond.acquire() #acquire the lock
        b = subprocess.Popen(['./start_call.sh'])
        cond.notifyAll()
        cond.release()
        time.sleep(self.sleeptime)

class Notifier(Thread):

    def __init__(self,condition,sleeptime=2):

        Thread.__init__(self)
        self.cond=condition
        self.sleeptime=sleeptime

    def run(self):
        cond=self.cond

    while 1:
        time.sleep(self.sleeptime)
        cond.acquire()
        cond.wait()
        print 'call has been generated'
        cond.release()

if __name__=="__main__":

    cond=Condition()
    call_gen=call_gen(cond)
    Not=Notifier(cond)
    call_gen.start()
    Not.start()
    while 1: pass

For information start_call.sh is a script that copy the 1.call and send it into the directory /var/spool…

Thanks to http://linuxgazette.net/107/pai.html made by Krishna G Pai that really helped!

Have fun.

Pas d'articles similaires.

Posted in asterisk, python. Bookmark the permalink. RSS feed for this post. Leave a trackback.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Protected by WP Anti Spam

Swedish Greys - a WordPress theme from Nordic Themepark.