我想知道如何限制这样的东西,一次只使用10个线程
with open("data.txt") as f: for line in f: lines = line.rstrip("\n\r") t1 = Thread(target=Checker, args=("company")) t1.start()
Shihab Shahr.. 5
使用Python的ThreadPoolExecutor,并将max_workers参数设置为10.
像这样:`
pool = ThreadPoolExecutor(max_workers=10) with open("data.txt") as f: for line in f: lines = line.rstrip("\n\r") pool.submit(Checker,"company") pool.shutdown(wait=True)
在pool
根据需要将自动分配的线程,限制分配的最大数目为10.第一个参数中pool.submit()
是函数名,参数被简称为逗号分隔值通过.
pool.shutdown(wait=True)
等待所有线程完成执行.
使用Python的ThreadPoolExecutor,并将max_workers参数设置为10.
像这样:`
pool = ThreadPoolExecutor(max_workers=10) with open("data.txt") as f: for line in f: lines = line.rstrip("\n\r") pool.submit(Checker,"company") pool.shutdown(wait=True)
在pool
根据需要将自动分配的线程,限制分配的最大数目为10.第一个参数中pool.submit()
是函数名,参数被简称为逗号分隔值通过.
pool.shutdown(wait=True)
等待所有线程完成执行.