1

I'm trying to get last value of symbol list from yahoo finance using thread in python. I read thread documentary however i don't understand that concept

#!/usr/bin/python
from threading import Thread
import urllib
import re

def th(ur):
 base = 'https://finance.yahoo.com/q?s='+ur
 htmlfile = urllib.urlopen(base).read()
 regex = '<span id="yfs_l84_[^.]*">(.+?)</span>'
 result = re.findall(regex,htmlfile)
 print result[0]

symbollist = open("symbollist.txt").read().splitlines()
threadlist = []
for u in symbollist:
 t = Thread(target=th,args=(u,))
 t.start()
 threadlist.append(t)
for b in threadlist:
 b.join()
I have a symbol list that contain 3000 symbol I don't know how many thread should i use or where should i lock threads
user2647541
  • 77
  • 1
  • 10
  • The answer to your question really depends what you want to do with this information. It may be better to just have your main program iterate through the list if printing them out is your goal. – SBH Jul 08 '15 at 21:32
  • I just wanna learn how to use thread in python and it's just sample project. – user2647541 Jul 08 '15 at 21:44
  • If you were learning to juggle would you start with 3000 balls? Create a simpler example with only a few threads and you'll learn more. – Will Jul 08 '15 at 22:22

2 Answers2

1

All code in your function called "th" would need to be thread safe.

If any of it isn't, you'll need to lock around those parts to stop "threading" issues, for example race conditions.

Will
  • 773
  • 1
  • 7
  • 24
1

Why are you using 3000 threads for 3000 symbols?

If you want to have a good impact on performance on mult-core machines by using multi-thread, you should use, in my opinion, approximately 8 or 10 thread. Each thread do numberOfJob/numberOfThread job.

The right way to limit maximum number of threads running at once?

Is the max thread limit actually a non-relevant issue for Python / Linux?

How many threads is too many?

Community
  • 1
  • 1
ridvanzoro
  • 646
  • 1
  • 10
  • 25