python 中的线程,需要帮助使程序根据另一个函数的条件等待
Threading in python, need help to make the program wait based on a condition on another function
我正在 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()
可以通过使用全局变量从目标函数返回来退出线程master_wish在以下示例中实现了这个技巧:
import threading
import time
master_wish = True # global variable
def kill_all_demons(demon):
while True:
print "killing %s\n" % demon
if not master_wish:
return
def van_helsing():
print "Yes Master your wish is my command"
t1 = threading.Thread(target=kill_all_demons, args=('Dracula',))
t2 = threading.Thread(target=kill_all_demons, args=('Wolf',))
t1.start()
t2.start()
def other_func():
global master_wish
master_wish = False
print("good will prevail over evil")
van_helsing()
time.sleep(10)
other_func()
我正在 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()
可以通过使用全局变量从目标函数返回来退出线程master_wish在以下示例中实现了这个技巧:
import threading
import time
master_wish = True # global variable
def kill_all_demons(demon):
while True:
print "killing %s\n" % demon
if not master_wish:
return
def van_helsing():
print "Yes Master your wish is my command"
t1 = threading.Thread(target=kill_all_demons, args=('Dracula',))
t2 = threading.Thread(target=kill_all_demons, args=('Wolf',))
t1.start()
t2.start()
def other_func():
global master_wish
master_wish = False
print("good will prevail over evil")
van_helsing()
time.sleep(10)
other_func()