Python 进程未通过块输入启动

问题描述 投票:0回答:1

最少的测试代码。如果清除 # time.sleep(0.5) 添加未在其他进程中启动的功能。没有 time.sleep 该怎么办

def add(counter):
    while 1:
        print(counter.value)
        counter.value += 1
        time.sleep(1)


if __name__ == '__main__':
    counter = multiprocessing.Value('i', 0)
    t = multiprocessing.Process(target=add, args=(counter,))
    t.start()
    #  time.sleep(0.5)
    input("blocking...")

我期望:0 1 2 等等。但我被阻塞了......

python python-3.x python-multiprocessing
1个回答
0
投票

您似乎忘记加入:

import multiprocessing
import time

def add(counter):
    while 1:
        print(counter.value)
        counter.value += 1
        time.sleep(1)
        
if __name__ == '__main__':
    counter = multiprocessing.Value('i', 0)
    t = multiprocessing.Process(target=add, args=(counter,))
    t.start()
    t.join()
    input("blocking...")
© www.soinside.com 2019 - 2024. All rights reserved.