Given your first description:
... any other solution may not have worked anyway. That is, if you
really expect the three threads to interleave their output as
abcabcabcabc. If threading, you have no assurance that any single thread
will follow any other during a task switch. It all depends upon where a
task switch takes place.
But then, your example isn't too clear of what you really are
producing for output. If, instead of "bytes", you meant that each thread
was writing logging information, the solution would be to use the
logging module -- so far as I know, the logging module /does/ perform
the needed access locking.
OTOH, if you really mean to have three separate byte producers,
interleaving output, the only safe way would be to have /each/ have an
output Queue, and a fourth thread doing the writing using a loop of the
form:
while True:
a = aQueue.get()
fout.write(a)
b = bQueue.get()
fout.write(b)
c = cQueue.get()
fout.write(c)
Using the separate queues means that the writer WILL wait until the
next producer in the interleave has produced its data. Of course, this
structure has the drawback that all producers must produce the same
amount of data -- otherwise it blocks forever on the queue from the
producer has stopped generating data.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/