[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

libc Sleep api performs a busy waiting

Mahesh

3/8/2010 8:59:00 AM

Hi,

I am having a problem while using sleep function from libc , the
thread in which i am calling it is getting struck and not allowing
other threads to execute. Here is a simple code that i am trying to
exeute

import threading
import time
import dl


def dummy1():
a=dl.open('/lib/libc.so.6')
print "in thread 1 Start"
a.call('sleep',2)
#time.sleep(2)
print "in thread 1 End"

def dummy2():
print "in thread 2 Start"
time.sleep(1)
print "in thread 2 End"
newThread1=threading.Thread(None,dummy1)
newThread2=threading.Thread(None,dummy2)
newThread1.start()
newThread2.start()

print "in main"



The out put of this program is (In this case thread 1 even though i
am calling a sleep function its not allowing other threads to execute,
other threads execute only after the completion of first thread)

in thread 1 Start
in thread 1 End
in thread 2 Start
in main
in thread 2 End


where as if i use time.sleep instead of a.call(sleep) the out put is
(which i guess is right behaviour, because it start the threads and
suspends them because the have sleep , and continue executing the main
thread)
in thread 1 Start
in thread 2 Start
in main
in thread 2 End
in thread 1 End


3 Answers

Steve Holden

3/8/2010 12:34:00 PM

0

Mahesh wrote:
> Hi,
>
> I am having a problem while using sleep function from libc , the
> thread in which i am calling it is getting struck and not allowing
> other threads to execute. Here is a simple code that i am trying to
> exeute
>
> import threading
> import time
> import dl
>
>
> def dummy1():
> a=dl.open('/lib/libc.so.6')
> print "in thread 1 Start"
> a.call('sleep',2)
> #time.sleep(2)
> print "in thread 1 End"
>
> def dummy2():
> print "in thread 2 Start"
> time.sleep(1)
> print "in thread 2 End"
> newThread1=threading.Thread(None,dummy1)
> newThread2=threading.Thread(None,dummy2)
> newThread1.start()
> newThread2.start()
>
> print "in main"
>
>
>
> The out put of this program is (In this case thread 1 even though i
> am calling a sleep function its not allowing other threads to execute,
> other threads execute only after the completion of first thread)
>
> in thread 1 Start
> in thread 1 End
> in thread 2 Start
> in main
> in thread 2 End
>
>
> where as if i use time.sleep instead of a.call(sleep) the out put is
> (which i guess is right behaviour, because it start the threads and
> suspends them because the have sleep , and continue executing the main
> thread)
> in thread 1 Start
> in thread 2 Start
> in main
> in thread 2 End
> in thread 1 End
>
>
Why not just use the time module's sleep function? Unlike the libc code
it releases Python's GIL (global interpreter lock), thus allowing other
threads to run.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

News123

3/8/2010 10:25:00 PM

0

Hi Steve,

Steve Holden wrote:
> Mahesh wrote:
>> Hi,
>>
>> I am having a problem while using sleep function from libc , the
>> thread in which i am calling it is getting struck and not allowing
>> other threads to execute. Here is a simple code that i am trying to
>> exeute
>>
>> import threading
>> import time
>> import dl
>>
>>
>> def dummy1():
>> a=dl.open('/lib/libc.so.6')
>> print "in thread 1 Start"
>> a.call('sleep',2)
>> #time.sleep(2)
>> print "in thread 1 End"
>>
>> def dummy2():
>> print "in thread 2 Start"
>> time.sleep(1)
>> print "in thread 2 End"
>> newThread1=threading.Thread(None,dummy1)
>> newThread2=threading.Thread(None,dummy2)
>> newThread1.start()
>> newThread2.start()
>>
>> print "in main"
>>
>>
>>
>> The out put of this program is (In this case thread 1 even though i
>> am calling a sleep function its not allowing other threads to execute,
>> other threads execute only after the completion of first thread)
>>
>> in thread 1 Start
>> in thread 1 End
>> in thread 2 Start
>> in main
>> in thread 2 End
>>
>>
>> where as if i use time.sleep instead of a.call(sleep) the out put is
>> (which i guess is right behaviour, because it start the threads and
>> suspends them because the have sleep , and continue executing the main
>> thread)
>> in thread 1 Start
>> in thread 2 Start
>> in main
>> in thread 2 End
>> in thread 1 End
>>
>>
> Why not just use the time module's sleep function? Unlike the libc code
> it releases Python's GIL (global interpreter lock), thus allowing other
> threads to run.
>
I fully agree for the given example.
Especcially time.sleep allows even sub second sleeps.

Out of curiousity though:

Is there any way to perform a call(), that releases the GIL before
calling the function?

It would avoid writing a wrapper?


bye


N

Thomas Heller

3/9/2010 4:43:00 PM

0

Joe Fox schrieb:
> Hi,
>
> actually i have simplified my scenario a lot here ,
>
> In my actual case , i have to call a C-api which blocks on c select , in a
> separate thread.
>
> my thread is getting struck in that api , and thus blocking all the other
> threads.
> Can you point to something which will help me call this blocking C-api call
> without my thread getting struck.
>
ctypes. Releases the GIL before calling C api functions.