multithreading

François Pinard pinard at iro.umontreal.ca
Fri May 24 18:03:15 EDT 2002


[Aahz]

> My response is that instead of trying to take advantage of the few
> atomic Python constructs, instead code defensively and always use
> thread-safe mechanisms for passing information.  Because Python has a
> powerful and simple Queue, this is straightforward to accomplish.

But abusing Queues for very simple things, a bit everywhere, might yield
code bloat, and impinge readability.  This is a bit like if someone was
inviting everyone to abuse fixed point integer arithmetic all over as a
way to program defensively against floating point arithmetic.

The key point is proper documentation.  Even saying that a behaviour is
undefined is good documentation, as it teaches what should be avoided.

One could be paranoid and setup queues and server to serve `os.listdir()',
say, in fear that two threads could not simultaneously use that library
function.  We can go overboard doing such things, maybe without any kind
of real necessity.  Best is to know how thing works, that is, what is
guaranteed and can be relied upon, and what is not guaranteed, and need
synchronisation mechanisms.  Mere testing is out of question, as a working
program is no proof of a correct usage.  Abusing of a few synchronisation
primitives is no good either: I hope being able to know Python well enough
to feel it is on my side, and not have to program defensively as if it
was a lost cause trying to understand how Python is meant to be used.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard






More information about the Python-list mailing list