1
我有一個由ID組成的list
,每天約50k。爲什麼我的線程停止?
,我必須每天進行50k的請求到服務器{服務器是在同一個城市},並獲取信息,並將其存儲到數據庫中。我已經做了,使用loop
和Threads
和我「已經注意到時間未知時間後它停止的讀取和存儲...
看看我的代碼片段
import re,urllib,urllib2
import mysql.connector as sql
import threading
from time import sleep
import idvalid
conn = sql.connect(user="example",password="example",host="127.0.0.1",database="students",collation="cp1256_general_ci")
cmds = conn.cursor()
ids=[] #here is going to be stored the ID's
def fetch():
while len(ids)>0:#it will loop until the list of ID's is finish
try:
idnumber = ids.pop()
content = urllib2.urlopen("http://www.example.com/fetch.php?id="+idnumber,timeout=120).read()
if content.find('<font color="red">') != -1:
pass
else:
name=content[-20:]
cmds.execute("INSERT INTO `students`.`basic` (`id` ,`name`)VALUES ('%s', '%s');"%(idnumber,name))
except Exception,r:
print r,"==>",idnumber
sleep(0.5)#i think sleep will help in threading ? i'm not sure
pass
print len(ids)#print how many ID's left
for i in range(0,50):#i've set 50 threads
threading.Thread(target=fetch).start()
output
的:它會繼續打印多少號的左,在未知的時刻它停止打印並提取&存儲
'mysql.connector'定義''1''threadsafety'](http://www.python.org/dev/peps/pep-0249/#threadsafety),這意味着你不應該在沒有同步的情況下在線程之間共享連接和遊標。 – mata
所以,我可以修改它嗎? – Hamoudaq
只是在'fetch'函數內移動'sql.connect'和'conn.cursor()' – mpaolini