Administrator
2023-09-25 b0dd299a9a9ad3e3ce3b19e6476d2155a0dfad6a
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
import multiprocessing
import queue
import time
 
__queue = queue.Queue()
 
 
def add_data(msg):
    time.sleep(1)
    start_time = time.time()
    __queue.put({"msg": msg})
    end_time = time.time()
    if end_time - start_time > 0.002:
        print("加入日志耗时")
 
 
def test_process_1(pipe):
    while True:
        for i in range(10):
            pipe.send_bytes(f"hello world:{i}".encode("utf-8"))
            time.sleep(1)
 
 
def test_process_2(pipe):
    while True:
        results = pipe.recv_bytes()
        if results:
            print("接受到内容:", results)
 
 
if __name__ == "__main__":
    p1, p2 = multiprocessing.Pipe()
    # L1订阅数据
    progress1 = multiprocessing.Process(target=lambda: test_process_1(p1))
    progress2 = multiprocessing.Process(target=lambda: test_process_2(p2))
    progress1.start()
    progress2.start()
 
    input()