<div dir="ltr">Late to answer. Seems no one did, so I&#39;d say don&#39;t write event listeners that run in a Python interpreter that FreeSWITCH knows nothing about and uses threading. <div><br></div><div>I looked at doing something similar in Python in the 1.2 days and after a couple of roadblocks like this, I took the advice of the wiki and the developers and developed what I needed in Lua. It worked out very well.</div><div><br></div><div>Python gets some love in Confluence, so I assume people use it, though. I know for a fact that you can start a Lua event listener by executing a luarun command via Python ESL API. Maybe it would also work in this case to run a Python script in the interpreter managed by mod_python. Let FS manage thread state.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 11, 2017 at 1:03 AM, Mohd Kamal Bin Mustafa <span dir="ltr">&lt;<a href="mailto:kamal.mustafa@gmail.com" target="_blank">kamal.mustafa@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have this python code:-<br>
<br>
import time<br>
import json<br>
import logging<br>
import threading<br>
<br>
from freeswitchESL import ESL<br>
<br>
class EventThread(threading.Thread):<br>
    def __init__(self, target, args):<br>
        super(EventThread, self).__init__()<br>
        self.target = target<br>
        self.args = args<br>
<br>
    def run(self):<br>
        print(&quot;Running target&quot;)<br>
        self.target(*self.args)<br>
<br>
def on_channel_park(e):<br>
    uuid = e.getHeader(&#39;Unique-ID&#39;)<br>
    print(&quot;Ringing&quot;)<br>
    conn.execute(&#39;ring_ready &#39;, &#39;&#39;, uuid)<br>
    conn.execute(&#39;sleep&#39;, &#39;5500&#39;, uuid)<br>
    conn.execute(&#39;answer&#39;, &#39;&#39;, uuid)<br>
    print(&quot;answered, playing media ...&quot;)<br>
    media_url = &#39;<a href="http://www.noiseaddicts.com/samples_1w72b820/17.mp3" rel="noreferrer" target="_blank">http://www.noiseaddicts.com/<wbr>samples_1w72b820/17.mp3</a>&#39;<br>
    resp = conn.execute(&quot;playback&quot;, media_url, uuid)<br>
    print(resp.getHeader(&quot;Reply-<wbr>Text&quot;))<br>
    print(&quot;playback done&quot;)<br>
    print(resp)<br>
<br>
if __name__ == &#39;__main__&#39;:<br>
    conn = ESL.ESLconnection(&#39;127.0.0.1&#39;, &#39;8021&#39;, &#39;mypass&#39;)<br>
    conn.events(&#39;plain&#39;, &#39;all&#39;)<br>
    while True:<br>
        e = conn.recvEvent()<br>
        uuid = e.getHeader(&#39;Unique-ID&#39;)<br>
        events_message = json.loads(e.serialize(&#39;json&#39;)<wbr>)<br>
        event_name = events_message[&#39;Event-Name&#39;].<wbr>lower()<br>
        method_name = &#39;on_%s&#39; % event_name<br>
        print(method_name)<br>
<br>
        if event_name == &#39;channel_park&#39;:<br>
            print(&quot;spawning thread&quot;)<br>
            t = EventThread(on_channel_park, args=(e,))<br>
            t.start()<br>
<br>
        if event_name == &#39;playback_stop&#39;:<br>
            conn.execute(&quot;hangup&quot;, &quot;&quot;, uuid)<br>
            print(&quot;Hanging up&quot;)<br>
<br>
Above, the code will stuck at conn.execute(&#39;ring_ready &#39;, &#39;&#39;, uuid) in<br>
on_channel_park(). It never went pass through until I hangup the call.<br>
In freeswitch log, it stopped at executing park().<br>
<br>
But if I moved ring_ready, sleep, answer outside of the thread it will<br>
work, i.e I can hear a playback:-<br>
<br>
def on_channel_park(e):<br>
    uuid = e.getHeader(&#39;Unique-ID&#39;)<br>
    print(&quot;answered, playing media ...&quot;)<br>
    media_url = &#39;<a href="http://www.noiseaddicts.com/samples_1w72b820/17.mp3" rel="noreferrer" target="_blank">http://www.noiseaddicts.com/<wbr>samples_1w72b820/17.mp3</a>&#39;<br>
    resp = conn.execute(&quot;playback&quot;, media_url, uuid)<br>
    print(resp.getHeader(&quot;Reply-<wbr>Text&quot;))<br>
    print(&quot;playback done&quot;)<br>
    print(resp)<br>
<br>
if __name__ == &#39;__main__&#39;:<br>
    conn = ESL.ESLconnection(&#39;127.0.0.1&#39;, &#39;8021&#39;, &#39;mypass&#39;)<br>
    conn.events(&#39;plain&#39;, &#39;all&#39;)<br>
    while True:<br>
        e = conn.recvEvent()<br>
        uuid = e.getHeader(&#39;Unique-ID&#39;)<br>
        events_message = json.loads(e.serialize(&#39;json&#39;)<wbr>)<br>
        event_name = events_message[&#39;Event-Name&#39;].<wbr>lower()<br>
        method_name = &#39;on_%s&#39; % event_name<br>
        print(method_name)<br>
<br>
        if event_name == &#39;channel_park&#39;:<br>
            print(&quot;Ringing&quot;)<br>
            conn.execute(&#39;ring_ready &#39;, &#39;&#39;, uuid)<br>
            conn.execute(&#39;sleep&#39;, &#39;5500&#39;, uuid)<br>
            conn.execute(&#39;answer&#39;, &#39;&#39;, uuid)<br>
            print(&quot;spawning thread&quot;)<br>
            t = EventThread(on_channel_park, args=(e,))<br>
            t.start()<br>
<br>
Any idea why this happen ?<br>
<br>
______________________________<wbr>______________________________<wbr>_____________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com" rel="noreferrer" target="_blank">http://www.<wbr>freeswitchsolutions.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org" rel="noreferrer" target="_blank">http://www.freeswitch.org</a><br>
<a href="http://confluence.freeswitch.org" rel="noreferrer" target="_blank">http://confluence.freeswitch.<wbr>org</a><br>
<a href="http://www.cluecon.com" rel="noreferrer" target="_blank">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org">FreeSWITCH-users@lists.<wbr>freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" rel="noreferrer" target="_blank">http://lists.freeswitch.org/<wbr>mailman/listinfo/freeswitch-<wbr>users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" rel="noreferrer" target="_blank">http://lists.<wbr>freeswitch.org/mailman/<wbr>options/freeswitch-users</a><br>
<a href="http://www.freeswitch.org" rel="noreferrer" target="_blank">http://www.freeswitch.org</a><br>
</blockquote></div><br></div>