[Tutor] Threads.. help /___\ (look of despair)
Hanna Joo
hanna@chagford.com
Mon, 11 Jun 2001 14:40:49 -0700
Hi all
I wrote a simple consumer - producer program trying to use threads (solid
emphasis on 'trying'). I have this feeling I don't know how to pass
arguments to threads and keep it synchronized.. any help will be greatly
appreaciated.
import threading
from time import sleep
def producer():
soup = 'ABCDEFGHIJKLMNOP'
while len(buff) != 15:
inx = randint(1,15)
buff.append(soup[inx])
print soup[inx], "thrown into soup!!"
sleep(randint(1,3))
def consumer():
while len(buff) :
a = buff.pop()
print a, "eaten from soup!!"
sleep(randint(1,3))
def main():
print 'starting threads...'
buff = []
firstthread = threading.Thread(target=producer, args=(buffer)) #---->
problem area!!
secondthread = threading.Thread(target=consumer, args=(buffer)) #---->
problem area!!
firstthread.start()
secondthread.start()
firstthread.join()
secondthread.join()
if __name__ == '__main__':
main()
ERROR>:
Exception in thread Thread-1:
Traceback (most recent call last):
File "c:\python\lib\threading.py", line 376, in __bootstrap
self.run()
File "c:\python\lib\threading.py", line 364, in run
apply(self.__target, self.__args, self.__kwargs)
TypeError: apply() 2nd argument must be a sequence
Exception in thread Thread-2:
Traceback (most recent call last):
File "c:\python\lib\threading.py", line 376, in __bootstrap
self.run()
File "c:\python\lib\threading.py", line 364, in run
apply(self.__target, self.__args, self.__kwargs)
TypeError: apply() 2nd argument must be a sequence
TIA
Hanna