0

I have the following situation

    def f(foo, bar, i):
        foo[i] = 42
        bar[i] = 76

    foo, bar = [[]]*24, [[]]*24
    g = partial(f, foo=foo, bar=bar)

    ... Call f in a multi-threaded way and have foo and bar filled ...

Foo and bar in the parent process could also be filled with objects rather than numericals.

How should I go about and do this in a safe way

This code is enclosed in a class method.

Roulbacha
  • 457
  • 7
  • 20
  • The most reliable approach would be using a pipe or queue. [docs](https://docs.python.org/3.7/library/multiprocessing.html#pipes-and-queues) – CMMCD Aug 16 '19 at 15:44
  • you can use a [Manager](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.sharedctypes.multiprocessing.Manager) to [share states between processes](https://docs.python.org/3/library/multiprocessing.html#sharing-state-between-processes) – j-i-l Aug 16 '19 at 16:42
  • The function is inside of a class and I cannot enclose it in the __name__ == __main__. – Roulbacha Aug 16 '19 at 17:02
  • whats the safe way you are refering? – Jainil Patel Aug 16 '19 at 18:00
  • No memory leak or crash – Roulbacha Aug 16 '19 at 19:13

1 Answers1

0

To accomplish what you would like to do the best way would be to use a Queue object to allow the child threads of your main loop to be able to pull in objects as they need to. This has good information for how to control information between processes in multithreaded python:

https://pymotw.com/2/multiprocessing/communication.html

huma474
  • 111
  • 3