Python Forum
Weird ProcessPoolExecutor initializer behavior
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird ProcessPoolExecutor initializer behavior
#1
In the snippet below I demonstrate how passing an instance method to ProcessPoolExecutor's initializer argument results in the assignment being 'lost' in the spawned process.

Is this by design?

from concurrent.futures import ProcessPoolExecutor, wait
from os import getpid


class Handler:

  def initialize(self):
    print(f"initialize {getpid()}")
    self.test = "one"

  def handle(self):
    try:
      test = self.test
    except AttributeError:
      print(f"handle {getpid()} error")
    else:
      print(f"handle {getpid()} {test}")


handler = Handler()

# this works, presumably because "one" is pickled before process is forked
#handler.initialize()

print('start')
with ProcessPoolExecutor(max_workers=4, initializer=handler.initialize) as ex:
  jobs = [ex.submit(handler.handle) for n in range(4)]
  wait(jobs)

print('done')
Reply
#2
Is this windows, linux, mac?

You say "spawned" and "pickled" so I am thinking windows, but you also say forked, and the way the code is written it cannot possibly be spawned. To run on windows you would do something like this:
from concurrent.futures import ProcessPoolExecutor, wait
from os import getpid


class Handler:
    def initialize(self):
        print(f"initialize {getpid()}")
        self.test = "one"

    def handle(self):
        try:
            test = self.test
        except AttributeError:
            print(f"handle {getpid()} error")
        else:
            print(f"handle {getpid()} {test}")


if __name__ == "__main__":
    handler = Handler()
    # this works, presumably because "one" is pickled before process is forked
    handler.initialize()

    print("start")
    with ProcessPoolExecutor(max_workers=4, initializer=handler.initialize) as ex:
        jobs = [ex.submit(handler.handle) for n in range(4)]
        wait(jobs)

    print("done")
What is your target OS?
Reply
#3
(Mar-13-2023, 04:30 PM)deanhystad Wrote: Is this windows, linux, mac?

You say "spawned" and "pickled" so I am thinking windows, but you also say forked, and the way the code is written it cannot possibly be spawned.
What is your target OS?

Linux. I tried both 'fork' and 'spawn' (via mp_context).

1. In both cases 'initialize' is called on the child pid (as expected).
2. With 'spawn' 'handle' is called on the parent pid (which seems wrong) actually I think this is just a quirk of getting the child pid
3. In both cases 'self.test' isn't set in 'handle' (which makes sense with spawn, but not with fork)


Thanks.

I also put it here: https://replit.com/@davetapley/ProcessPo...ug#main.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,907 Nov-12-2021, 09:55 PM
Last Post: snippsat
  python nested list assignment weird behavior eyalk1 2 4,463 Jan-16-2018, 07:32 PM
Last Post: wavic
  Weird behavior with Led Driving code rgkaizen 0 2,714 Jul-14-2017, 04:23 AM
Last Post: rgkaizen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020