Python proccess not start via block input

32 views Asked by At

Minimal code for test. If clear # time.sleep(0.5) to add function not started in other proccess.How do without time.sleep

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()
    #  time.sleep(0.5)
    input("blocking...")

I expected: 0 1 2 and etc. But i get blocking...

Update:
Works in the console. If you run it through Pycharm windows, the proccess is blocked. settings_pycharm.png

1

There are 1 answers

6
Carlos On

Looks like you forgot to join:

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...")