I posted this originally to the python-list[1], but then discovered that my problem only occurs in PythonWin. Any help would be appreciated:
[1] http://mail.python.org/pipermail/python-list/2004-December/257666.html -------- Original Message -------- Subject: calling functions across threads Date: Wed, 29 Dec 2004 17:35:41 GMT From: Steven Bethard <[EMAIL PROTECTED]> Newsgroups: comp.lang.python I'm playing around with some threading stuff right now, and I'm having a little trouble calling a function from one thread that affects another. Here's my setup: py> import os, threading, time py> def write(file_in, input_lines): ... for line in input_lines: ... time.sleep(0.5) ... file_in.write(line) ... file_in.flush() ... file_in.close() ... py> def read(file_out, output_list): ... while True: ... line = file_out.readline() ... if not line: ... break ... output_list.append(line) ... py> def runthreads(lst): ... file_in, file_out, file_err = os.popen3('cat') ... write_thread = threading.Thread( ... target=write, args=(file_in, ... ['%s\n' % x for x in range(10)])) ... read_thread = threading.Thread(target=read, ... args=(file_out, lst)) ... write_thread.start() ... read_thread.start() ... write_thread.join() ... read_thread.join() ... Basically, I start one thread to read and one thread to write (from a os.pipe). This all works fine for me: py> lst = [] py> runthreads(lst) py> lst ['0\n', '1\n', '2\n', '3\n', '4\n', '5\n', '6\n', '7\n', '8\n', '9\n'] I run into a problem though when I try to call an update method every time I read a line: py> class updatinglist(list): ... def __init__(self, updater): ... super(updatinglist, self).__init__() ... self.updater = updater ... def append(self, item): ... super(updatinglist, self).append(item) ... self.updater(len(self)) ... py> def update(i): ... print i ... py> lst = updatinglist(update) py> runthreads(lst) 1 2 3 4 5 6 7 8 9 10 py> lst ['0\n', '1\n', '2\n', '3\n', '4\n', '5\n', '6\n', '7\n', '8\n', '9\n'] I get the correct output, but if you run this yourself, you'll see that the numbers 1 through 10 aren't printed in sync with the writes (i.e. every half second); they're all printed at the end. Could someone explain to me why this happens, and how (if possible) I can get the numbers printed in sync with the appends to the list? Thanks, Steve _______________________________________________ Python-win32 mailing list Python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32