0x01 安装vthread函数库
系统命令行下执行:
pip install vthread
0x02 一句话实现简单多线程
1 2 3 4 5 6 7 8 9 10 11
| import vthread,requests
@vthread.thread(5) def compete(url): r = requests.get(url) if r.status_code == 200 : print("[*]Success") else: print("[*]Fail. Retrying...")
compete("http://www.baidu.com/")
|
相同效果:
1 2 3 4 5 6 7 8 9 10 11 12
| import vthread,requests
@vthread.thread def compete(url): r = requests.get(url) if r.status_code == 200 : print("[*]Success") else: print("[*]Fail. Retrying...")
for i in range(5): compete("http://www.baidu.com/")
|
0x03 线程池包装
1 2 3 4 5 6 7 8 9 10 11 12
| import vthread,requests
@vthread.pool(10) def compete(url): r = requests.get(url) if r.status_code == 200 : print("[*]Success") else: print("[*]Fail. Retrying...")
for i in range(20): compete("http://www.baidu.com/")
|
不加装饰器就是普通的单线程,只用加一行就能在不破坏原来的结构直接实现线程池操作,能进行参数传递,支持分组,这已经到了不破坏代码的极限了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import time import vthread
@vthread.pool(6) def foolfunc(num): time.sleep(1) print(f"foolstring, test2 foolnumb: {num}")
for i in range(10): foolfunc(i)
执行效果如下: [ Thread-1 ] foolstring, test2 foolnumb: 0 [ Thread-5 ] foolstring, test2 foolnumb: 4 [ Thread-2 ] foolstring, test2 foolnumb: 2 [ Thread-6 ] foolstring, test2 foolnumb: 5 [ Thread-4 ] foolstring, test2 foolnumb: 3 [ Thread-3 ] foolstring, test2 foolnumb: 1 [ Thread-1 ] foolstring, test2 foolnumb: 6 [ Thread-5 ] foolstring, test2 foolnumb: 7 [ Thread-2 ] foolstring, test2 foolnumb: 8 [ Thread-6 ] foolstring, test2 foolnumb: 9
|
如果你想要让你的某几个函数有M个线程执行,而另外几个函数要N个线程去执行。 那么请看看下面的使用说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import time import vthread
pool_1 = vthread.pool(5,gqueue=1) pool_2 = vthread.pool(2,gqueue=2)
@pool_1 def foolfunc1(num): time.sleep(1) print(f"foolstring1, test3 foolnumb1:{num}")
@pool_2 def foolfunc2(num): time.sleep(1) print(f"foolstring2, test3 foolnumb2:{num}") @pool_2 def foolfunc3(num): time.sleep(1) print(f"foolstring3, test3 foolnumb3:{num}")
for i in range(10): foolfunc1(i) for i in range(10): foolfunc2(i) for i in range(10): foolfunc3(i)
|
如果需要考虑对函数内的某些步骤进行锁的操作,那么请看下面的使用说明。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
import time import vthread
@vthread.thread(5) def foolfunc_():
@vthread.atom def do_some_fool_thing1(): pass @vthread.atom def do_some_fool_thing2(): pass
do_some_fool_thing1() do_some_fool_thing2()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
|
假如在使用过程中装饰了多个函数会怎么计算线程池的线程数量呢? 这里给出了说明,在 vthread.pool 函数库中,是以 gqueue 这个参数来确定线程池分组的。 而相同的分组,则会默认使用最后一个 “人为定义” 的伺服线程数量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
@vthread.pool(10) def foolfunc1(): pass @vthread.pool(18) def foolfunc1(): pass
@vthread.pool(10) def foolfunc1(): pass @vthread.pool() def foolfunc1(): pass
@vthread.pool() def foolfunc1(): pass @vthread.pool() def foolfunc1(): pass 这样就意味着gqueue=0的线程池数量为默认的cpu核心数
pool1 = vthread.pool(gqueue=1) pool2 = vthread.pool(6,gqueue=2) pool2 = vthread.pool(8,gqueue=2)
|
考虑到函数库的多用性,可能是觉得这种直接粗暴的开启多线程函数的测试需求比较常见,所以会保留有这样的一个功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import time import vthread
@vthread.thread(5) def foolfunc(num): time.sleep(1) print(f"foolstring, test1 foolnumb: {num}")
foolfunc(123)
执行效果如下: [ Thread-1 ] foolstring, test1 foolnumb: 123 [ Thread-2 ] foolstring, test1 foolnumb: 123 [ Thread-3 ] foolstring, test1 foolnumb: 123 [ Thread-4 ] foolstring, test1 foolnumb: 123 [ Thread-5 ] foolstring, test1 foolnumb: 123
@vthread.thread def foolfunc(num): time.sleep(1) print(f"foolstring, test1 foolnumb: {num}")
for i in range(5): foolfunc(123)
执行效果同上
|