Administrator
2025-06-09 8b7972581d0324e3f634b5b5a57a9ed7db1addaf
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
42
43
44
45
46
47
48
49
50
51
52
53
import logging
import multiprocessing
import time
 
 
def run_process1(queue: multiprocessing.Queue):
    while True:
        try:
            queue.put_nowait("process1")
            time.sleep(1)
        except Exception as e:
            logging.exception(e)
 
 
def run_process2(queue: multiprocessing.Queue):
    while True:
        try:
            queue.put_nowait("process2")
            time.sleep(1)
        except Exception as e:
            logging.exception(e)
 
 
def run_process3(queue: multiprocessing.Queue):
    while True:
        try:
            print(queue.get())
            time.sleep(0.001)
        except Exception as e:
            logging.exception(e)
 
if __name__ == "__main__":
    q = multiprocessing.Queue(maxsize=1024)
    while True:
        try:
            val = q.get_nowait()
            print(val)
        except:
            time.sleep(0.005)
            q.put_nowait("123123")
            print("出错")
 
if __name__ == "__main__1":
    q = multiprocessing.Queue(maxsize=1024)
    p1 = multiprocessing.Process(target=run_process1, args=(q,))
    p2 = multiprocessing.Process(target=run_process2, args=(q,))
    p3 = multiprocessing.Process(target=run_process3, args=(q,))
    p1.start()
    p2.start()
    p3.start()
 
    while True:
        time.sleep(0.1)