最好的线程池包。
vthread的Python项目详细描述
您可以通过添加一行代码来实现线程池,而无需更改任何先前代码的顺序。
importvthread@vthread.pool(3)# just use this line to make pool, Create a threadpool with three threadsdefcrawl(i):importtime;time.sleep(1)# Simulation time consumingprint("crawl_url:",i)urls=["http://url1","http://url2","http://url3","http://url4"]foruinurls:crawl(u)# This function becomes a function that adds the original function to the thread pool.
它提供了对线程池进行分组的方法
importvthreadpool_1=vthread.pool(5,gqueue=1)# open a threadpool with 5 threads named 1pool_2=vthread.pool(2,gqueue=2)# open a threadpool with 2 threads named 2@pool_1deffoolfunc1(num):time.sleep(1)print(f"foolstring1, test3 foolnumb1:{num}")@pool_2deffoolfunc2(num):time.sleep(1)print(f"foolstring2, test3 foolnumb2:{num}")@pool_2deffoolfunc3(num):time.sleep(1)print(f"foolstring3, test3 foolnumb3:{num}")foriinrange(10):foolfunc1(i)foriinrange(4):foolfunc2(i)foriinrange(2):foolfunc3(i)# default gqueue is 0