Background:
I am working on a project that uses Django with a Postgres database. We also use mod_wsgi in case this matters, as some of my internet searches mention this. In the web form view, the Django view launches a task that will take a considerable amount of time (more than the user would like to wait), so we start the work with a system call in the background. The task that is now being executed should be able to read and write to the database. Since this work takes a lot of time, we use multiprocessing for parallel operation of its parts.
Problem:
The top level script has a database connection, and when it spawns child processes, it seems that the parent connection is accessible to children. Then, an exception is thrown before the call because the INSULATION LEVEL SET TRANSACTION needs to be called. Studies have shown that this is due to an attempt to use the same database connection in several processes. One thread that I found suggested calling connection.close () at the beginning of the child processes so that Django would automatically create a new connection when it needed it, and so each child process would have a unique connection, i.e. not shared. This did not work for me, since calling connection.close () in the child process made the parent process complain that the connection was lost.
Other findings:
Some of the things I read seemed to indicate that you cannot do this, and that multiprocessing, mod_wsgi and Django do not mix well. It seems to me that it seems to me that it seems to me that I think.
Some of them suggest using celery, which can be a long-term solution, but I canβt install celery at this time, until some approval procedures are considered, so now itβs not an option.
Found several links to SO and elsewhere about persistent database connections, which I think are another problem.
Also found links to psycopg2.pool and pgpool and something like a bouncer. Admittedly, I did not understand much of what I read on these issues, but of course he did not jump at me as what I was looking for.
Current "Work-Around":
Currently, I will return to simply triggering events sequentially, and it works, but more slowly than I would like.
Any suggestions on how I can use multiprocessing for parallel work? It seems that if my parent and two children had independent connections to the database, everything would be fine, but I can not understand what this behavior is.
Thank you and sorry for the length!