重写select逻辑,benchmark结果看来提升了225倍

This commit is contained in:
2024-10-20 22:06:03 +08:00
parent 8a2f07fcf1
commit 8e8cedc778
6 changed files with 128 additions and 23 deletions

View File

@ -1,3 +1,4 @@
import time
from multiprocessing import Process
from magicoca import Chan, select
@ -13,7 +14,6 @@ def sp2(chan: Chan[int]):
chan << i << i * 3
def rp(chans: list[Chan[int]]):
rl = []
for t in select(*chans):
@ -22,21 +22,23 @@ def rp(chans: list[Chan[int]]):
break
print(rl)
def send_process(chan: Chan[int], _id: int):
while True:
chan << _id
time.sleep(2)
def recv_process(chan_list: list[Chan[int]]):
for t in select(*chan_list):
print(t)
class TestSelect:
def test_select(self):
chan1 = Chan[int]()
chan2 = Chan[int]()
print("Test Chan Select")
p1 = Process(target=sp1, args=(chan1,))
p2 = Process(target=sp2, args=(chan2,))
p3 = Process(target=rp, args=([chan1, chan2],))
p3.start()
p1.start()
p2.start()
p1.join()
p2.join()
p3.join()
chan_list = []
for i in range(10):
chan = Chan[int]()
chan_list.append(chan)
p = Process(target=send_process, args=(chan, i))
p.start()
p = Process(target=recv_process, args=(chan_list,))
p.start()