Fork from Django to separate process
If you have some code that takes long-time blocking initialization (initialize, read something create mappings) you should start it in another process.
parent_conn = None
class YourAppNameConfig(AppConfig):
name = 'your_app_name'
def ready(self):
from multiprocessing import Process, Pipe
from django import db
# When using multiple databases, you should close all connections.
for name, info in db.connections.databases.items(): # Close the DB connections
db.connection.close()
global parent_conn
parent_conn, child_conn = Pipe()
p = Process(target=some_init_function, args=(child_conn,))
p.daemon = True
p.start()
"""
Add this if you need to restart this process
"""
@staticmethod
def restart_process():
from multiprocessing import Process, Pipe
global parent_conn
if parent_conn:
parent_conn.send(-1)
parent_conn, child_conn = Pipe()
YourAppNameConfig.p = Process(target=one_licencee_name_matcher, args=(child_conn,))
YourAppNameConfig.p.daemon = True
YourAppNameConfig.p.start()
def some_init_function(child_conn):
_some_slow_code
while True:
input_arg = pipe.recv()
if plant_name:
if plant_name == -1:
sys.exit(0)
"""
output_result = something(input_arg )
""""
process_input_data
pipe.send(output_result)
So here we created process and Pipe
to communicate with it.
So now we can communicate with process
def process_communicate(arg):
parent_conn.send(arg)
return parent_conn.recv()
To correctly finish process with parent we can add SIGTERM handler:
def signal_handler(signal, frame):
if parent_conn:
parent_conn.send(-1)
sys.exit(0)
signal.signal(signal.SIGTERM, signal_handler)