Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Queue.Queue-like class without the busy-wait

Reply
Thread Tools

Queue.Queue-like class without the busy-wait

 
 
Paul L. Du Bois
Guest
Posts: n/a
 
      03-24-2005
Has anyone written a Queue.Queue replacement that avoids busy-waiting?
It doesn't matter if it uses os-specific APIs (eg
WaitForMultipleObjects). I did some googling around and haven't found
anything so far.

Because I know someone will ask: no, the busy-waiting hasn't been a
problem in my app. I'm just interested in reading the code.

p

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      03-25-2005
Paul L. Du Bois wrote:
> Has anyone written a Queue.Queue replacement that avoids busy-waiting?
> It doesn't matter if it uses os-specific APIs (eg
> WaitForMultipleObjects). I did some googling around and haven't found
> anything so far.
>
> Because I know someone will ask: no, the busy-waiting hasn't been a
> problem in my app. I'm just interested in reading the code.


I don't believe the term "busy-wait" applies here.
Busy-waiting is (in my experience) where you run in
tight loops without releasing the CPU, consuming all
CPU time available.

I'm fairly certain that while Queue (and other things
that wait in Python) does have a loop, you'll find that
it's not a busy-waiting loop and you definitely don't
get 100% CPU usage during the wait. It goes to sleep
for increasingly large periods of time while waiting,
if the code in threading._Condition is any indication.

-Peter
 
Reply With Quote
 
 
 
 
Paul L. Du Bois
Guest
Posts: n/a
 
      03-25-2005
Peter Hansen wrote:
> I don't believe the term "busy-wait" applies here.
> [Explanation]


Yes, well, you're right. I was thinking of calling it
"slacker-waiting" but didn't want to come off too cute.

p

 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      03-25-2005
Op 2005-03-24, Paul L. Du Bois schreef <(E-Mail Removed)>:
> Has anyone written a Queue.Queue replacement that avoids busy-waiting?
> It doesn't matter if it uses os-specific APIs (eg
> WaitForMultipleObjects). I did some googling around and haven't found
> anything so far.


I started once, using the Timer class in the Threading Module to
break the lock. However the Timer class uses the same kind of
sleep-polling loop, to delay the exection and allow an intermediate
cancel, as the loop that is used in Queue.Queue, so that was no
gain.

I have still played with the idea, but haven't worked anything out
since then.

--
Antoon Pardon
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-25-2005
Antoon Pardon <(E-Mail Removed)> writes:
> I started once, using the Timer class in the Threading Module to
> break the lock. However the Timer class uses the same kind of
> sleep-polling loop, to delay the exection and allow an intermediate
> cancel, as the loop that is used in Queue.Queue, so that was no
> gain.


I've never checked this code but it wouldn't have occurred to me that
Queue uses any kind of timeout loop. Can't it work the obvious way
with a semaphore?
 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      03-25-2005
Op 2005-03-25, Paul Rubin schreef <http>:
> Antoon Pardon <(E-Mail Removed)> writes:
>> I started once, using the Timer class in the Threading Module to
>> break the lock. However the Timer class uses the same kind of
>> sleep-polling loop, to delay the exection and allow an intermediate
>> cancel, as the loop that is used in Queue.Queue, so that was no
>> gain.

>
> I've never checked this code but it wouldn't have occurred to me that
> Queue uses any kind of timeout loop. Can't it work the obvious way
> with a semaphore?


And how is this semaphore going to be released if the timeout is
reached?

--
Antoon Pardon
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-25-2005
Antoon Pardon <(E-Mail Removed)> writes:
> > I've never checked this code but it wouldn't have occurred to me that
> > Queue uses any kind of timeout loop. Can't it work the obvious way
> > with a semaphore?

>
> And how is this semaphore going to be released if the timeout is
> reached?


I meant a semaphore to synchronize the queue when adding or removing
objects. Timeout would be handled with sigalarm or select.

 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      03-25-2005
Op 2005-03-25, Paul Rubin schreef <http>:
> Antoon Pardon <(E-Mail Removed)> writes:
>> > I've never checked this code but it wouldn't have occurred to me that
>> > Queue uses any kind of timeout loop. Can't it work the obvious way
>> > with a semaphore?

>>
>> And how is this semaphore going to be released if the timeout is
>> reached?

>
> I meant a semaphore to synchronize the queue when adding or removing
> objects.


Last I looked there was a lock used for that.

The loop is only for when you cant remove or add an element immediatly
and there is a timeout.

> Timeout would be handled with sigalarm or select.


How is select going to help? IMO you can't put a Queue in a select call.
And it is doubtfull if working with sigalarm will do the trick.

First of all is the problem the signal module in python is very limited.
IIRC all signals are routed to the main thread. So breaking a lock
by having the thread signaled is impossible in python.

You may provide your own signal module, but that may not be enough.
The last time I experimented with a pthreads in C, locks didn't
break by signalling the thread. That might be a bug, but I wouldn't
know since I'm not familiar with the pthread specifications.

--
Antoon Pardon

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-25-2005
Antoon Pardon <(E-Mail Removed)> writes:
> > I meant a semaphore to synchronize the queue when adding or removing
> > objects.

>
> Last I looked there was a lock used for that.


OK, that amounts to the same thing.

> The loop is only for when you cant remove or add an element immediatly
> and there is a timeout.


> > Timeout would be handled with sigalarm or select.

>
> How is select going to help? IMO you can't put a Queue in a select call.
> And it is doubtfull if working with sigalarm will do the trick.


You could open a socket to your own loopback port and then select on
it, or something like that. The select call takes a timeout parameter.

> First of all is the problem the signal module in python is very limited.
> IIRC all signals are routed to the main thread. So breaking a lock
> by having the thread signaled is impossible in python.


A signal handler in the main thread could release a lock that the
thread is waiting on.
 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      03-25-2005
Op 2005-03-25, Paul Rubin schreef <http>:
> Antoon Pardon <(E-Mail Removed)> writes:
>> > I meant a semaphore to synchronize the queue when adding or removing
>> > objects.

>>
>> Last I looked there was a lock used for that.

>
> OK, that amounts to the same thing.
>
>> The loop is only for when you cant remove or add an element immediatly
>> and there is a timeout.

>
>> > Timeout would be handled with sigalarm or select.

>>
>> How is select going to help? IMO you can't put a Queue in a select call.
>> And it is doubtfull if working with sigalarm will do the trick.

>
> You could open a socket to your own loopback port and then select on
> it, or something like that. The select call takes a timeout parameter.


Well maybe you could use an os.pipe as a timeout lock then. When the lock is
instantiated you put one byte in it. Aquiring the lock is implemented by
reading one byte, releasing the lock is implemented by writing a byte.
Aquiring the lock with a timeout would make use of select.

It would require carefull coding, since you want to prevent the thread
blocking because of select returning indicating it could be read, but
between the select and the actual read an other thread already consumed
the byte.

>> First of all is the problem the signal module in python is very limited.
>> IIRC all signals are routed to the main thread. So breaking a lock
>> by having the thread signaled is impossible in python.

>
> A signal handler in the main thread could release a lock that the
> thread is waiting on.


This wouldn't work. A thread would have no way knowing for what
purpose the lock was released, because the lock was released
by the thread holding the lock or because the signal handler
released the lock, both would look the same for the thread
aquiring the lock.

There is also the problem you can't direct which thread will
aquire the lock. Suppose you have two threads waiting on a
lock, one plain, one with a timeout. Your signal handler
kicks in and releases the lock. There is a good chance
the first thread will now aquire the thread and the thread
that used a timeout will continue to be blocked.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiding the public members of a class without modifying the CLASS Brahmam ASP .Net 3 01-11-2006 12:00 AM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments