F
F
FewSeconds2021-09-03 14:40:48
Python
FewSeconds, 2021-09-03 14:40:48

How to solve the problem with multiprocessing?

Hi all. Faced the following problem: there is a certain function that is called multi-threaded, and there is code that should be executed as usual (synchronously) after the function has finished, but everything goes wrong: after the function is called, the code that should be executed synchronously, multi-threaded, and the whole program turns into a mess. Please tell me how to solve this problem and make multiprocessing apply only to the function that is assigned to it, and not to the entire program. Here is an example code:

from concurrent.futures import ProcessPoolExecutor as Pool


def example(string):
  print(string*2)

### пределы в которых мультипроцессинг начинает свою работу

if __name__ == "__main__":
  with Pool(3) as p:
    p.map(example, ["2131", "213123123", "12312312312", "1231231231"])

### пределы в которых мультипроцессинг должен оканчивать свою работу и програма продолжала работу в обычном режиме

"""
Какой-то код, который должен выполняться синхронно как обычно...
"""

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-09-03
@FewSeconds

First, it is necessary to be defined all the same multithreaded or multiprocessing. Secondly, your synchronous code is most likely located at the top level of the source code, and then executed when multiprocessing creates new script processes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question