On May 8, 2:13 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On 7 May 2007 18:22:07 -0700, est <[EMAIL PROTECTED]> declaimed > the following in comp.lang.python: > > > > > I guess I will write multiple files, one thread one file, and after > > all threads terminates, I combine the files. That's a cheaper > > solution, I think. > > Given your first description: > > > I need to write a file using 3 threads simutaniously, e.g. Thread 1 > > write the first byte of test.bin with an "a", second thread write the > > second byte "b", third thread write the third byte "c". Anyone could > > give a little example on how to do that? > > ... 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 > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/
I'd like to say VERY VERY VERY thank you for your detailed information, that's a lot encourage for a beginner. In fact I am writing a multi thread download ultility, and I found that very hard for me. Can you recommand any sample code where I can start with? I hope I can catch up with everyone here, I'll try my best learning python. Thank you again. -- http://mail.python.org/mailman/listinfo/python-list