K
K
Kirill Petrov2020-04-18 14:14:08
Python
Kirill Petrov, 2020-04-18 14:14:08

How to subscribe to an event from a process and address data to a specific process in python 3?

Greetings! Decided to learn Python. Can you tell me how to properly subscribe to the data change event in the process?

Tried something like this:

import multiprocessing as mp
import logging
import time

def foo(q):
    i = 1
    while True:
        q.put({'yndx': i})
        time.sleep(1)
        i += 1

if __name__ == '__main__':
    mp.log_to_stderr().setLevel(logging.DEBUG)
    mp.set_start_method('spawn')
    q = mp.Queue()
    p = mp.Process(target=foo, args=(q,))
    p.start()
    while True:
        if not q.empty():
            broadcast = q.get()
            print(broadcast)


But here the infinite loop loads the percent to the maximum, and when there are a lot of them ... I don’t want to use time.sleep in an infinite loop, since instant data delivery is required.

And in the end, the task is that one process receives the data, and other processor processes take what they need. At the same time, their number changes during the life of the program.
5e9ae058a0664391499429.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Petrov, 2020-04-19
@Recosh

Figured it out myself. To receive an event that there is new data, I use multiprocessing.Event(), and to exchange datamultiprocessing.Manager().dict()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question