Re: Threading issue in Python example

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You can’t use time.sleep.

I solved it by using the loop listed below.

 

    # Non blocking delayLoop

    def delayLoop (delay, delta=100):

        start_time = time.time()

        while time.time()- start_time < delay:

            self.ep.libHandleEvents(delta)

 

Any event that happens will be processed even if your main loop (calling this delayLoop) will be paused.

libHandleEvents can return at any point so you can’t use that command as a predictable delay.

 

/ Mats

 

From: pjsip <pjsip-bounces@xxxxxxxxxxxxxxx> On Behalf Of Mike Taylor via pjsip
Sent: den 4 februari 2020 22:04
To: pjsip@xxxxxxxxxxxxxxx
Cc: Mike Taylor <mtaylor@xxxxxxxxxx>
Subject: Threading issue in Python example

 

Greetings,

 

I have read about the threading issues with using Python but I don’t quite understand
what I can do to properly synchronize the main in a simple script with, for example, the registration
state of a SIP account.  In trying to understand how to do it I have found that even the
simple Python demo at https://www.pjsip.org/docs/book-latest/html/intro_pjsua2.html
will cause a seg fault due to the time.sleep() call.  Note that for the script to work you must
add import pjsua as pj and import time to it.  But that’s mainly all I did and of course I changed
the SIP registrar URI to my SIP proxy.  Oh, and I updated the print to be compatible with
python3.  Maybe running with python3 is an issue?  Anyway, if I take out the time.sleep

call then no seg fault.  But with it I get a seg fault.

I discovered this after frustrating hours trying to post a semaphore from the onRegState
callback and wait on the semaphore in the main thread.  I thought it was something to
do with the use of the semaphore but then removed that and tried just running with
the thread.sleep and to my surprise that was enough to cause the seg fault. 
Just making the one-line change to my script to comment out the call to thread.sleep
as below makes it register successfully with my proxy.

    …

    acc = Account();

    acc.create(acfg);

  # Here we don't have anything else to do..

  time.sleep(10);

 

The preceding works causes a seg fault

But the following does not and all works wonderfully:

 

    acc = Account();

    acc.create(acfg);

  # Here we don't have anything else to do..

  # time.sleep(10);

 

 

Thanks,

Mike


This email may contain information which is privileged or protected against unauthorized disclosure or communication. If you are not the intended recipient, please notify the sender and delete this message and any attachments from your system without producing, distributing or retaining copies thereof or disclosing its contents to any other person.

Telia Company processes emails and other files that may contain personal data in accordance with Telia Company’s Privacy Policy.



_______________________________________________
Visit our blog: http://blog.pjsip.org

pjsip mailing list
pjsip@xxxxxxxxxxxxxxx
http://lists.pjsip.org/mailman/listinfo/pjsip_lists.pjsip.org

[Index of Archives]     [Asterisk Users]     [Asterisk App Development]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [Linux API]
  Powered by Linux