2024-10-20 22:06:03 +08:00
|
|
|
import time
|
2024-10-13 01:56:28 +08:00
|
|
|
from multiprocessing import Process
|
|
|
|
|
|
|
|
from magicoca import Chan, select
|
|
|
|
|
|
|
|
|
2024-10-20 22:06:03 +08:00
|
|
|
def send_process(chan: Chan[int], _id: int):
|
2025-02-18 06:33:33 +08:00
|
|
|
for i in range(10):
|
|
|
|
chan << i
|
|
|
|
time.sleep(0.1 * _id)
|
2024-10-13 01:56:28 +08:00
|
|
|
|
2024-10-20 22:06:03 +08:00
|
|
|
def recv_process(chan_list: list[Chan[int]]):
|
2025-02-18 06:33:33 +08:00
|
|
|
c = []
|
2024-10-20 22:06:03 +08:00
|
|
|
for t in select(*chan_list):
|
2025-02-18 06:33:33 +08:00
|
|
|
c.append(t)
|
|
|
|
print("Select", t)
|
|
|
|
if len(c) == 30:
|
|
|
|
break
|
2024-10-20 22:06:03 +08:00
|
|
|
class TestSelect:
|
|
|
|
def test_select(self):
|
2025-02-18 06:33:33 +08:00
|
|
|
ch1 = Chan[int]()
|
|
|
|
ch2 = Chan[int]()
|
|
|
|
ch3 = Chan[int]()
|
|
|
|
|
|
|
|
p1 = Process(target=send_process, args=(ch1, 1))
|
|
|
|
p2 = Process(target=send_process, args=(ch2, 2))
|
|
|
|
p3 = Process(target=send_process, args=(ch3, 3))
|
|
|
|
p4 = Process(target=recv_process, args=([ch1, ch2, ch3],))
|
|
|
|
|
|
|
|
p1.start()
|
|
|
|
p2.start()
|
|
|
|
p3.start()
|
|
|
|
p4.start()
|
|
|
|
|
|
|
|
p1.join()
|
|
|
|
p2.join()
|
|
|
|
p3.join()
|
|
|
|
p4.join()
|