calling functions across threads
It's me
itsme at yahoo.com
Wed Dec 29 21:23:48 EST 2004
I haven't play with the thread stuff in Python (yet) but in general terms
(from a C mind), one should not expect read/write actions to be sequential
across threads. I would assume the Python threads eventually goes back to
some system calls for thread handling. If that were the case, you should
not be surprised at all that the I/O sequences appear to be quite
unpredictable.
If you absolutely, positively wants them to come out in a certain way, you
need to build in additionally serialization mechanisims in your code (like
use semaphores and stuff).
"Steven Bethard" <steven.bethard at gmail.com> wrote in message
news:NVBAd.280309$V41.151261 at attbi_s52...
> 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