Threading modeule and condition??

I

Ishwar Rattan

System is Mandrake-9.1 with Python-2.3.4

I need to learn condition variables (mentioned in Section 7.5.3 of library
reference) but I am confused on the way of creating one. The documentation
says:
---
# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()

To choose between notify() and notifyAll(), consider whether one state
change can be interesting for only one or several waiting threads. E.g. in
a typical producer-consumer situation, adding one item to the buffer only
needs to wake up one consumer thread.

class Condition([lock])

If the lock argument is given and not None, it must be a Lock or RLock
object, and it is used as the underlying lock. Otherwise, a new RLock object
is created and used as the underlying lock.
---

how do I insttantiate 'cv' the condition object in the first place. A code
fragment will be helpful.

-ishwar
 
C

Christopher T King

how do I insttantiate 'cv' the condition object in the first place. A code
fragment will be helpful.

Just like any object ;)

Just make sure you do this _before_ creating your threads, so that the
object is shared by all the threads when they are created.
 
D

David Bolen

Christopher T King said:
Just make sure you do this _before_ creating your threads, so that the
object is shared by all the threads when they are created.

Order of creation shouldn't make any difference. Condition objects
(as all of the synchronization objects built on top of the lock
primitive) exist independent of thread's per-se. They are designed
for use from multiple-threads, so it doesn't matter if they are
created before or after the threads that use them, although obviously
you need to hand the threads references to the objects they are going
to use.

-- David
 
C

Christopher T King

Order of creation shouldn't make any difference. Condition objects
(as all of the synchronization objects built on top of the lock
primitive) exist independent of thread's per-se. They are designed
for use from multiple-threads, so it doesn't matter if they are
created before or after the threads that use them, although obviously
you need to hand the threads references to the objects they are going
to use.

That's more what I was getting at, "make sure you create one and pass it
to all your threads instead of creating different ones in each thread".
Poor wording on my part.
 
A

Aahz

I need to learn condition variables (mentioned in Section 7.5.3 of library
reference) but I am confused on the way of creating one.

Right. That's why you don't use conditions. Use Queue.Queue instead.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"To me vi is Zen. To use vi is to practice zen. Every command is a
koan. Profound to the user, unintelligible to the uninitiated. You
discover truth everytime you use it." (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top