python - Threads not working -


i trying out following code learn threading in python.

import urllib.request import re import threading sys import argv, exit  if len(argv[1:])==0:     exit("you haven't entered arguments. try again.") else:     comps=argv[1:]  def extr(comp):                          url = 'http://finance.yahoo.com/q?s='+comp     req = urllib.request.request(url)     resp = urllib.request.urlopen(req)     respdata = resp.read()     print (re.findall(r'<span id="yfs_l84_[^.]*">(.*?)</span>',str(respdata)))   x in comps:     t = threading.thread(extr(x))     t.daemon = true     t.start() 

i right result 1 after other , not @ once. missing something?

t = threading.thread(extr(x)) problem. calling extr(x), , passing result of thread constructor. try thread(target=extr, args=(x,)).

you'll need use https://docs.python.org/2/library/queue.html allow threads pass result data main thread before terminate. you'd create queue in main thread, , pass argument each subthread.


Comments