我在所有服务器中都有200个SQL服务器和40个相同的表。我想在所有200个线程中每个表执行python数据管道。只想知道我是否可以一次并发地运行10个线程,如果它们完成了,它将运行下一个10个线程,直到200个线程在一个作业中自动完成。
for index, row in shops.iterrows():
tf =isOpen(row['shop_ip'] , row['port'])
endTime = time.time()
pingTime = endTime-startTime
if tf:
print(f"UP {row['shop_ip']} Ping Successful Time Taken : "+str(pingTime)+" seconds")
x = threading.Thread(target=ETLScript.ETLLoadingShopPos,args=(SelectColumns,tableName,tableName,row['shop_code'],'where 1=1',str(row['shop_code']),row))
jobs.append(x)
x.start()
x.join()发布于 2022-05-27 04:18:13
根据您的解释,是将10个线程作为一个运行组运行,还是只允许10个线程运行?
我认为排队的概念是可行的
import time
import threading
jobs = []
for index, row in shops.iterrows():
tf =isOpen(row['shop_ip'] , row['port'])
endTime = time.time()
pingTime = endTime-startTime
if tf:
while sum([j.is_alive() for j in jobs]) >= 10:
time.sleep(0.3) # give it a range to check to avoid resource consumed the whole time by main thread
print(f"UP {row['shop_ip']} Ping Successful Time Taken : "+str(pingTime)+" seconds")
x = threading.Thread(target=ETLScript.ETLLoadingShopPos,args=(SelectColumns,tableName,tableName,row['shop_code'],'where 1=1',str(row['shop_code']),row))
jobs.append(x)
x.start()这允许您一次运行10个线程。while循环将用作阻止程序,以检查这些线程是否已经完成,将启动另一个新线程。
https://stackoverflow.com/questions/72395678
复制相似问题