[python] [queue] Verify that the queue will not lose data under multi-process - reliable

In order to ensure the stability of the program running under multiple processes, the queue queue is needed, but the queue must be 100% reliable, so verify whether the queue is absolutely stable; the script mainly verifies whether there is any
missing data;

Authentication method:

1. At least 2 (30 scripts) processes write data to the queue
2. One process reads data
to ensure that the read data must be consistent with the amount of written data. If they are not consistent, the queue will be unreliable

# -*- encoding: utf-8 -*-
"""
验证queue在多线程put是没有问题的
queue在多线程时不会丢数据
"""
import sys
import re
import time
from multiprocessing import Process, cpu_count, Queue

put_num = 200000
put_multiprocess = 30
get_num = put_num * put_multiprocess


def test_queue():
    q = Queue()
    puts = []
    for i in range(put_multiprocess):
        puts.append(Process(target=multiprocess_puts, args=(q,)))

    get = Process(target=multiprocess_get, args=(q,))
    get.start()
    for p in puts:
        p.start()

    for p in puts:
        p.join()
    get.join()


def multiprocess_puts(q):
    for i in range(put_num):
        q.put(i)


def multiprocess_get(q):
    # las
    for i in range(get_num):
        q.get()
        print(i)
        if i + 1 // 1000 == 0:
            print(i)


if __name__ == '__main__':
    test_queue()

Guess you like

Origin blog.csdn.net/qq_39057568/article/details/132149401