0

I am running a trigger function on INSERT/UPDATE that would create a new process that sends a post request to an api.

on a Ubuntu + PostgresQL 12 docker container running I was able to get the new process to form without an issue with the below code

pid=os.fork()
... do some logic
req = urllib2.Request(apiURI)
f = urllib2.urlopen(req)

Now Attempting the same on my windows machine, its clear fork is not an option.

What is best practice when running multiprocessing on a windows system?

Denis S Dujota
  • 543
  • 5
  • 13

1 Answers1

2

fork() is not supported by windows.

You can achieve the same using the multiprocessing module:

from multiprocessing import Process

def foo():
    print 'hello'

if __name__ == '__main__':
    p = Process(target=foo)
    p.start()
Helio Santos
  • 6,606
  • 3
  • 25
  • 31
  • Ok i see, so make sure that the attribute 'foo' is available before spawning new process. how scalable is this type of solution? should i look into `from multiprocessing import Pool`? – Denis S Dujota May 14 '20 at 17:10
  • If you need several processes that are a good option. I would also take a look at ProcessPoolExecuter if you are running python 3.2+ https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor – Helio Santos May 14 '20 at 17:30
  • interesting, so ProcessPoolExecuter wraps Pool with some helpful abilities. thanks will look more into this. is there any time we would choose pool over processPoolExecutor or does it not matter? – Denis S Dujota May 14 '20 at 17:45
  • processPoolExecutor it's easier to use, I find e.g. it comes with a context manager. – Helio Santos May 14 '20 at 17:51