calling functions across threads

Steven Bethard steven.bethard at gmail.com
Wed Dec 29 18:35:41 CET 2004


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



More information about the Python-list mailing list