我正在使用线程在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()
答案 0 :(得分:1)
可以通过使用全局变量从目标函数返回来退出线程master_wish在下面的示例中做了诀窍:
Uncaught Error: Minified exception occurred; use the non-minified dev
environment for the full error message and additional helpful warnings.