Skip to content Skip to sidebar Skip to footer

How To Use Multiprocess In Django Command?

I'm tring to use ProcessPoolExecutor in django command to get some results at same time. And I tried with below codes to get it # main codes import json import time import dateti

Solution 1:

You'll need to set up Django for the subprocesses in the subprocess initializer function.

def subprocess_setup():
    django.setup()
    # Could do other things here

# ...

withProcessPoolExecutor(max_workers=5, initializer=subprocess_setup) as executor:
   

Solution 2:

Got the same issue but solved it differently as suggested by this thread.

I had to explicitly pass the context to the ProcessPoolExecutor in order to get things right. The code would look something like this

import multiprocessing

fork_context = multiprocessing.get_context('fork')
with ProcessPoolExecutor(max_workers=5, mp_context=fork_context) as executor:
...

Post a Comment for "How To Use Multiprocess In Django Command?"