Threading issue in Python example

MT
Mike Taylor
Tue, Feb 4, 2020 9:03 PM

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

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
BM
Bergman, Mats
Wed, Feb 5, 2020 8:30 PM

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@lists.pjsip.org On Behalf Of Mike Taylor via pjsip
Sent: den 4 februari 2020 22:04
To: pjsip@lists.pjsip.org
Cc: Mike Taylor mtaylor@unicoi.com
Subject: [pjsip] 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 Policyhttps://www.teliacompany.com/en/about-the-company/privacy/.

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@lists.pjsip.org> On Behalf Of Mike Taylor via pjsip Sent: den 4 februari 2020 22:04 To: pjsip@lists.pjsip.org Cc: Mike Taylor <mtaylor@unicoi.com> Subject: [pjsip] 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<https://www.teliacompany.com/en/about-the-company/privacy/>.
MT
Mike Taylor
Wed, Feb 5, 2020 11:28 PM

Hi Mats,

I discovered that the issue I was having was because I had
not uncommented the following line in
pjproject-2.9/pjsip-apps/src/swig/python/Makefile

before building pjsip:

#USE_THREADS = -threads -DSWIG_NO_EXPORT_ITERATOR_METHODS

Once I did that and rebuilt and reinstalled the python package the issue was
resolved.

/Mike

From: Bergman, Mats [mailto:mats.bergman@teliacompany.com]
Sent: Wednesday, February 05, 2020 12:31 PM
To: pjsip list
Cc: Mike Taylor
Subject: RE: [pjsip] Threading issue in Python example

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@lists.pjsip.org On Behalf Of Mike Taylor via
pjsip
Sent: den 4 februari 2020 22:04
To: pjsip@lists.pjsip.org
Cc: Mike Taylor mtaylor@unicoi.com
Subject: [pjsip] 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
https://www.teliacompany.com/en/about-the-company/privacy/ .

Hi Mats, I discovered that the issue I was having was because I had not uncommented the following line in pjproject-2.9/pjsip-apps/src/swig/python/Makefile before building pjsip: #USE_THREADS = -threads -DSWIG_NO_EXPORT_ITERATOR_METHODS Once I did that and rebuilt and reinstalled the python package the issue was resolved. /Mike From: Bergman, Mats [mailto:mats.bergman@teliacompany.com] Sent: Wednesday, February 05, 2020 12:31 PM To: pjsip list Cc: Mike Taylor Subject: RE: [pjsip] Threading issue in Python example 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@lists.pjsip.org> On Behalf Of Mike Taylor via pjsip Sent: den 4 februari 2020 22:04 To: pjsip@lists.pjsip.org Cc: Mike Taylor <mtaylor@unicoi.com> Subject: [pjsip] 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 <https://www.teliacompany.com/en/about-the-company/privacy/> .