[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

Queue peek?

Veloz

3/2/2010 4:29:00 PM

Hi all
I'm looking for a queue that I can use with multiprocessing, which has
a peek method.

I've seen some discussion about queue.peek but don't see anything in
the docs about it.

Does python have a queue class with peek semantics?

Michael
14 Answers

Raymond Hettinger

3/2/2010 6:19:00 PM

0

On Mar 2, 8:29 am, Veloz <michaelve...@gmail.com> wrote:
> Hi all
> I'm looking for a queue that I can use with multiprocessing, which has
> a peek method.
>
> I've seen some discussion about queue.peek but don't see anything in
> the docs about it.
>
> Does python have a queue class with peek semantics?

Am curious about your use case? Why peek at something
that could be gone by the time you want to use it.

val = q.peek()
if something_i_want(val):
v2 = q.get() # this could be different than val

Wouldn't it be better to just get() the value and return if you don't
need it?

val = q.peek()
if not something_i_want(val):
q.put(val)


Raymond



Veloz

3/2/2010 7:02:00 PM

0

On Mar 2, 1:18 pm, Raymond Hettinger <pyt...@rcn.com> wrote:
> On Mar 2, 8:29 am, Veloz <michaelve...@gmail.com> wrote:
>
> > Hi all
> > I'm looking for a queue that I can use with multiprocessing, which has
> > a peek method.
>
> > I've seen some discussion about queue.peek but don't see anything in
> > the docs about it.
>
> > Does python have a queue class with peek semantics?
>
> Am curious about your use case?  Why peek at something
> that could be gone by the time you want to use it.
>
>   val = q.peek()
>   if something_i_want(val):
>        v2 = q.get()         # this could be different than val
>
> Wouldn't it be better to just get() the value and return if you don't
> need it?
>
>   val = q.peek()
>   if not something_i_want(val):
>       q.put(val)
>
> Raymond

Yeah, I hear you. Perhaps queue is not the best solution. My highest
level use case is this: The user visits a web page (my app is a
Pylons app) and requests a "report" be created. The report takes too
long to create and display on the spot, so the user expects to visit
some url "later" and see if the specific report has completed, and if
so, have it returned to them.

At a lower level, I'm thinking of using some process workers to create
these reports in the background; there'd be a request queue (into
which requests for reports would go, each with an ID) and a completion
queue, into which the workers would write an entry when a report was
created, along with an ID matching the original request.

The "peek" parts comes in when the user comes back later to see if
their report has done. That is, in my page controller logic, I'd like
to look through the complete queue and see if the specific report has
been finished (I could tell by matching up the ID of the original
request to the ID in the completed queue). If there was an item in the
queue matching the ID, it would be removed.

It's since occurred to me that perhaps a queue is not the best way to
handle the completions. (We're ignoring the file system as a solution
for the time being, and focusing on in-memory structures). I'm
wondering now if a simple array of completed items wouldn't be better.
Of course, all the access to the array would have to be thread/process-
proof. As you pointed out, for example, multi-part operations such as
"is such-and-such an ID in the list? If so, remove it and return in"
would have to be treated atomically to avoid concurrency issues.

Any thoughts on this design approach are welcomed :-)
Michael

MRAB

3/2/2010 7:45:00 PM

0

Veloz wrote:
> On Mar 2, 1:18 pm, Raymond Hettinger <pyt...@rcn.com> wrote:
>> On Mar 2, 8:29 am, Veloz <michaelve...@gmail.com> wrote:
>>
>>> Hi all
>>> I'm looking for a queue that I can use with multiprocessing, which has
>>> a peek method.
>>> I've seen some discussion about queue.peek but don't see anything in
>>> the docs about it.
>>> Does python have a queue class with peek semantics?
>> Am curious about your use case? Why peek at something
>> that could be gone by the time you want to use it.
>>
>> val = q.peek()
>> if something_i_want(val):
>> v2 = q.get() # this could be different than val
>>
>> Wouldn't it be better to just get() the value and return if you don't
>> need it?
>>
>> val = q.peek()
>> if not something_i_want(val):
>> q.put(val)
>>
>> Raymond
>
> Yeah, I hear you. Perhaps queue is not the best solution. My highest
> level use case is this: The user visits a web page (my app is a
> Pylons app) and requests a "report" be created. The report takes too
> long to create and display on the spot, so the user expects to visit
> some url "later" and see if the specific report has completed, and if
> so, have it returned to them.
>
> At a lower level, I'm thinking of using some process workers to create
> these reports in the background; there'd be a request queue (into
> which requests for reports would go, each with an ID) and a completion
> queue, into which the workers would write an entry when a report was
> created, along with an ID matching the original request.
>
> The "peek" parts comes in when the user comes back later to see if
> their report has done. That is, in my page controller logic, I'd like
> to look through the complete queue and see if the specific report has
> been finished (I could tell by matching up the ID of the original
> request to the ID in the completed queue). If there was an item in the
> queue matching the ID, it would be removed.
>
> It's since occurred to me that perhaps a queue is not the best way to
> handle the completions. (We're ignoring the file system as a solution
> for the time being, and focusing on in-memory structures). I'm
> wondering now if a simple array of completed items wouldn't be better.
> Of course, all the access to the array would have to be thread/process-
> proof. As you pointed out, for example, multi-part operations such as
> "is such-and-such an ID in the list? If so, remove it and return in"
> would have to be treated atomically to avoid concurrency issues.
>
> Any thoughts on this design approach are welcomed :-)
>
A set of completed reports, or a dict with the ID as the key? The
advantage of a dict is that the value could contain several bits of
information, such as when it was completed, the status (OK or failed),
etc. You might want to wrap it in a class with locks (mutexes) to ensure
it's threadsafe.

Martin P. Hellwig

3/2/2010 7:58:00 PM

0

On 03/02/10 19:44, MRAB wrote:
<cut>
> information, such as when it was completed, the status (OK or failed),
> etc. You might want to wrap it in a class with locks (mutexes) to ensure
> it's threadsafe.
What actually happens if multiple threads at the same time, write to a
shared dictionary (Not using the same key)?

I would think that if the hashing part of the dictionary has some sort
of serialization (please forgive me if I misuse a term) it should 'just
work'(tm)?

--
mph

mk

3/2/2010 9:55:00 PM

0

Daniel Stutzbach wrote:
> On Tue, Mar 2, 2010 at 1:58 PM, Martin P. Hellwig
> <martin.hellwig@dcuktec.org <mailto:martin.hellwig@dcuktec.org>> wrote:
>
> What actually happens if multiple threads at the same time, write to
> a shared dictionary (Not using the same key)?

> All of Python's built-in types are thread safe. Both updates will happen.

No need to use synchro primitives like locks?

I know that it may work, but that strikes me as somehow wrong... I'm
used to using things like Lock().acquire() and Lock().release() when
accessing shared data structures, whatever they are.

Although trying to do the "right thing" may indeed get one in trouble in
case of deadlock caused by a bug in one's own program.

Regards,
mk


John Krukoff

3/2/2010 10:09:00 PM

0

On Tue, 2010-03-02 at 22:54 +0100, mk wrote:
<snip>
> No need to use synchro primitives like locks?
>
> I know that it may work, but that strikes me as somehow wrong... I'm
> used to using things like Lock().acquire() and Lock().release() when
> accessing shared data structures, whatever they are.
<snip>

This is one of those places where the GIL is a good thing, and makes
your life simpler. You could consider it that the interpreter does the
locking for you for such primitive operations, if you like.
--
John Krukoff <jkrukoff@ltgc.com>
Land Title Guarantee Company

MRAB

3/3/2010 1:00:00 AM

0

John Krukoff wrote:
> On Tue, 2010-03-02 at 22:54 +0100, mk wrote:
> <snip>
>> No need to use synchro primitives like locks?
>>
>> I know that it may work, but that strikes me as somehow wrong... I'm
>> used to using things like Lock().acquire() and Lock().release() when
>> accessing shared data structures, whatever they are.
> <snip>
>
> This is one of those places where the GIL is a good thing, and makes
> your life simpler. You could consider it that the interpreter does the
> locking for you for such primitive operations, if you like.

I suppose it depends on the complexity of the data structure. A dict's
methods are threadsafe, for example, but if you have a data structure
where access leads to multiple method calls then collectively they need
a lock.

Gregory Ewing

3/3/2010 6:15:00 AM

0

MRAB wrote:

> I suppose it depends on the complexity of the data structure. A dict's
> methods are threadsafe, for example, but if you have a data structure
> where access leads to multiple method calls then collectively they need
> a lock.

It also depends on the nature of the objects being used
as dict keys. Dict operations invoke the hashing and
comparison methods of the keys, which in general can
execute arbitrary Python code.

If the keys are elementary built-in types such as
strings or ints, then dict operations will *probably*
be atomic. But I think I'd use a lock anyway, to be
on the safe side.

--
Greg

Veloz

3/3/2010 2:58:00 PM

0

On Mar 3, 1:14 am, Gregory Ewing <greg.ew...@canterbury.ac.nz> wrote:
> MRAB wrote:
> > I suppose it depends on the complexity of the data structure. A dict's
> > methods are threadsafe, for example, but if you have a data structure
> > where access leads to multiple method calls then collectively they need
> > a lock.
>
> It also depends on the nature of the objects being used
> as dict keys. Dict operations invoke the hashing and
> comparison methods of the keys, which in general can
> execute arbitrary Python code.
>
> If the keys are elementary built-in types such as
> strings or ints, then dict operations will *probably*
> be atomic. But I think I'd use a lock anyway, to be
> on the safe side.
>
> --
> Greg

Unless I missed where you guys were going, I think we got off the main
point. The main question at hand was this: what's the best way (heck,
any way) to implement a sort of "peek" whereby a number of processes
can write results to some common "object" and some other process can
"peek" into this object, looking for specific items they're interested
in?

I've tried doing this with a queue, as follows: children all write
results to queue, each result has an identifier. Another interested
party, which wants to know if identifier XXX has been placed in the
queue, removes all the items, one by one from the queue, "keeps" the
one matching the identifier (if found) and puts the rest of the items
back on the queue, so other interested parties can also look through
it.

This is not a good solution, but it illustrates what I'm trying to
achieve..

I'm looking at multiprocessing.Manager, ctypes, etc, but nothing's
really jumped out.

I also tried creating my own list class which uses locks to provide a
"peek and remove" method, but I don't have a good way to share an
instance of this object across processes.

Any thoughts would be welcomed!
Michael

Steve Holden

3/3/2010 3:42:00 PM

0

Veloz wrote:
> On Mar 3, 1:14 am, Gregory Ewing <greg.ew...@canterbury.ac.nz> wrote:
>> MRAB wrote:
>>> I suppose it depends on the complexity of the data structure. A dict's
>>> methods are threadsafe, for example, but if you have a data structure
>>> where access leads to multiple method calls then collectively they need
>>> a lock.
>> It also depends on the nature of the objects being used
>> as dict keys. Dict operations invoke the hashing and
>> comparison methods of the keys, which in general can
>> execute arbitrary Python code.
>>
>> If the keys are elementary built-in types such as
>> strings or ints, then dict operations will *probably*
>> be atomic. But I think I'd use a lock anyway, to be
>> on the safe side.
>>
>> --
>> Greg
>
> Unless I missed where you guys were going, I think we got off the main
> point. The main question at hand was this: what's the best way (heck,
> any way) to implement a sort of "peek" whereby a number of processes
> can write results to some common "object" and some other process can
> "peek" into this object, looking for specific items they're interested
> in?
>
> I've tried doing this with a queue, as follows: children all write
> results to queue, each result has an identifier. Another interested
> party, which wants to know if identifier XXX has been placed in the
> queue, removes all the items, one by one from the queue, "keeps" the
> one matching the identifier (if found) and puts the rest of the items
> back on the queue, so other interested parties can also look through
> it.
>
> This is not a good solution, but it illustrates what I'm trying to
> achieve..
>
> I'm looking at multiprocessing.Manager, ctypes, etc, but nothing's
> really jumped out.
>
> I also tried creating my own list class which uses locks to provide a
> "peek and remove" method, but I don't have a good way to share an
> instance of this object across processes.
>
> Any thoughts would be welcomed!
> Michael

Sounds to me like you are using one queue when you should be using
several. If a single process wants to distribute information to multiple
clients, have a queue for each client. This makes it easy for each
client to find out if it has work (if it does its queue is non-empty),

Is this helpful, or a red herring?

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...