我試圖運行此代碼:Python的主題:無法啓動新的線程
def VideoHandler(id):
try:
cursor = conn.cursor()
print "Doing {0}".format(id)
data = 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)
picture = 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 with {0}".format(id)
cursor.execute("insert into videos (`title`, `picture`, `link`, `vid_id`) values('{0}', '{1}', '{2}', {3})".format(title, picture, link, id))
print "Added {0} to the database".format(id)
except:
pass
x = 1
while True:
if x != 945719:
currentX = x
thread.start_new_thread(VideoHandler, (currentX))
else:
break
x += 1
,它說「無法啓動新的線程」
我不是線程專家,但我相當確信您的計算機將具有有限的尋址空間,即不太可能您可以創建945718個併發線程。此外,您可以嘗試更好地格式化您的代碼,因爲它不會以所示的格式運行。 – Raceyman
我們能否看到回溯(不僅是「無法啓動新的線程」,這太過於通用)?建議:在你的try塊內部的操作之前,我建議使用lock.acquire()和lock.release(),因爲你正在對數據庫執行操作(所以這是一個關鍵部分) –
做了答案我提議解決你的問題?如果是這樣,請接受它。 –