0
我想在Python中使用線程的東西。這是我第一次使用線程。所以任何幫助,將不勝感激。Python中的線程化,需要幫助,使程序等待根據另一個函數的條件
所以這裏是事情。我開始10個線程在另一個函數中執行某些操作。但是如果在其他函數中遇到了一些條件,我應該可以在所有這些線程上調用join()。所以我需要找到一種方法將線程對象傳遞給其他函數。有人可以幫助我找到一個方法。
def db_routine():
#global t1, t2, t3, t4, t5, t6, t7, t8, t9, t0
connection = pymysql.connect(user='root', password='', host='127.0.0.1', database='franchisedb')
try:
with connection.cursor() as cursor:
sql = "select number, url, address, flag from `colleges_com`"
cursor.execute(sql)
#connection.commit()
row=cursor.fetchone()
while row is not None:
if row[3] == 1:
print("Item already processed.")
row = cursor.fetchone()
continue
if row[2] != None:
print("Address exists for the entry number: " + str(row[0]))
row = cursor.fetchone()
else:
print("Adding address for the entry number: " + str(row[0]))
time.sleep(.5)
if row[0]%10 == 1:
t1 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t1.start()
row = cursor.fetchone()
elif row[0]%10 == 2:
t2 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t2.start()
row = cursor.fetchone()
elif row[0]%10 == 3:
t3 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t3.start()
row = cursor.fetchone()
elif row[0]%10 == 4:
t4 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t4.start()
row = cursor.fetchone()
elif row[0]%10 == 5:
t5 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t5.start()
row = cursor.fetchone()
elif row[0]%10 == 6:
t6 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t6.start()
row = cursor.fetchone()
elif row[0]%10 == 7:
t7 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t7.start()
row = cursor.fetchone()
elif row[0]%10 == 8:
t8 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t8.start()
row = cursor.fetchone()
elif row[0]%10 == 9:
t9 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t9.start()
row = cursor.fetchone()
elif row[0]%10 == 0:
t0 = threading.Thread(target=goto_url, args = (row[0], row[1]))
time.sleep(2)
t0.start()
row = cursor.fetchone()
finally:
connection.close()
cursor.close()
感謝的人。我不知道爲什麼我沒有想到這個! :) – defiant