Python Thread: can't start new thread -



Python Thread: can't start new thread -

i'm trying run code:

def videohandler(id): try: cursor = conn.cursor() print "doing {0}".format(id) info = urllib2.urlopen("http://myblogfms2.fxp.co.il/video" + str(id) + "/").read() title = re.search("<span class=\"style5\"><strong>([\\s\\s]+?)</strong></span>", data).group(1) image = re.search("#4f9eff;\"><img src=\"(.+?)\" width=\"120\" height=\"90\"", data).group(1) link = re.search("flashvars=\"([\\s\\s]+?)\" width=\"612\"", data).group(1) id = id print "done {0}".format(id) cursor.execute("insert videos (`title`, `picture`, `link`, `vid_id`) values('{0}', '{1}', '{2}', {3})".format(title, picture, link, id)) print "added {0} database".format(id) except: pass x = 1 while true: if x != 945719: currentx = x thread.start_new_thread(videohandler, (currentx)) else: break x += 1

and says "can't start new thread"

the real reason error create way many threads (more 100k!!!) , nail os-level limit.

your code can improved in many ways besides this:

don't utilize low level thread module, utilize thread class in threading module. join threads @ end of code limit number of threads create reasonable: process elements, create little number of threads , allow each 1 process subset of whole info (this propose below, adopt producer-consumer pattern worker threads getting info queue.queue instance) and never, ever have except: pass statement in code. or if do, don't come crying here if code not work , cannot figure out why. :-)

here's proposal:

from threading import thread import urllib2 import re def videohandler(id_list): id in id_list: try: cursor = conn.cursor() print "doing {0}".format(id) info = urllib2.urlopen("http://myblogfms2.fxp.co.il/video" + str(id) + "/").read() title = re.search("<span class=\"style5\"><strong>([\\s\\s]+?)</strong></span>", data).group(1) image = re.search("#4f9eff;\"><img src=\"(.+?)\" width=\"120\" height=\"90\"", data).group(1) link = re.search("flashvars=\"([\\s\\s]+?)\" width=\"612\"", data).group(1) id = id print "done {0}".format(id) cursor.execute("insert videos (`title`, `picture`, `link`, `vid_id`) values('{0}', '{1}', '{2}', {3})".format(title, picture, link, id)) print "added {0} database".format(id) except: import traceback traceback.print_exc() conn = get_some_dbapi_connection() threads = [] nb_threads = 8 max_id = 945718 in range(nb_threads): id_range = range(i*max_id//nb_threads, (i+1)*max_id//nb_threads + 1) thread = thread(target=videohandler, args=(id_range,)) threads.append(thread) thread.start() thread in threads: thread.join() # wait completion

python

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -