Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7962463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:20:31+00:00 2026-06-04T05:20:31+00:00

Background: I’m working a project which uses Django with a Postgres database. We’re also

  • 0

Background:

I’m working a project which uses Django with a Postgres database. We’re also using mod_wsgi in case that matters, since some of my web searches have made mention of it. On web form submit, the Django view kicks off a job that will take a substantial amount of time (more than the user would want to wait), so we kick off the job via a system call in the background. The job that is now running needs to be able to read and write to the database. Because this job takes so long, we use multiprocessing to run parts of it in parallel.

Problem:

The top level script has a database connection, and when it spawns off child processes, it seems that the parent’s connection is available to the children. Then there’s an exception about how SET TRANSACTION ISOLATION LEVEL must be called before a query. Research has indicated that this is due to trying to use the same database connection in multiple processes. One thread I found suggested calling connection.close() at the start of the child processes so that Django will automatically create a new connection when it needs one, and therefore each child process will have a unique connection – i.e. not shared. This didn’t work for me, as calling connection.close() in the child process caused the parent process to complain that the connection was lost.

Other Findings:

Some stuff I read seemed to indicate you can’t really do this, and that multiprocessing, mod_wsgi, and Django don’t play well together. That just seems hard to believe I guess.

Some suggested using celery, which might be a long term solution, but I am unable to get celery installed at this time, pending some approval processes, so not an option right now.

Found several references on SO and elsewhere about persistent database connections, which I believe to be a different problem.

Also found references to psycopg2.pool and pgpool and something about bouncer. Admittedly, I didn’t understand most of what I was reading on those, but it certainly didn’t jump out at me as being what I was looking for.

Current “Work-Around”:

For now, I’ve reverted to just running things serially, and it works, but is slower than I’d like.

Any suggestions as to how I can use multiprocessing to run in parallel? Seems like if I could have the parent and two children all have independent connections to the database, things would be ok, but I can’t seem to get that behavior.

Thanks, and sorry for the length!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:20:32+00:00Added an answer on June 4, 2026 at 5:20 am

    Multiprocessing copies connection objects between processes because it forks processes, and therefore copies all the file descriptors of the parent process. That being said, a connection to the SQL server is just a file, you can see it in linux under /proc//fd/…. any open file will be shared between forked processes. You can find more about forking here.

    My solution was just simply close db connection just before launching processes, each process recreate connection itself when it will need one (tested in django 1.4):

    from django import db
    db.connections.close_all()
    def db_worker():      
        some_paralell_code()
    Process(target = db_worker,args = ())
    

    Pgbouncer/pgpool is not connected with threads in a meaning of multiprocessing. It’s rather solution for not closing connection on each request = speeding up connecting to postgres while under high load.

    Update:

    To completely remove problems with database connection simply move all logic connected with database to db_worker – I wanted to pass QueryDict as an argument… Better idea is simply pass list of ids… See QueryDict and values_list(‘id’, flat=True), and do not forget to turn it to list! list(QueryDict) before passing to db_worker. Thanks to that we do not copy models database connection.

    def db_worker(models_ids):        
        obj = PartModelWorkerClass(model_ids) # here You do Model.objects.filter(id__in = model_ids)
        obj.run()
    
    
    model_ids = Model.objects.all().values_list('id', flat=True)
    model_ids = list(model_ids) # cast to list
    process_count = 5
    delta = (len(model_ids) / process_count) + 1
    
    # do all the db stuff here ...
    
    # here you can close db connection
    from django import db
    db.connections.close_all()
    
    for it in range(0:process_count):
        Process(target = db_worker,args = (model_ids[it*delta:(it+1)*delta]))   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I'm writing an app that uses the ViewPager and that's working nicely. I
Background: I am developing a web-based teaching program (using Drupal 7, if that matters)
Background I am writing code in VS 2010, .NET 4, C#. Also, in case
Background: At my company we are developing a bunch applications that are using the
Background This is only my second PyQt4 project. Developing a Windows app that has
Background My project is urgent and requires that I iterate a large XML file
BACKGROUND I'm using VS 2010 on a machine where I installed .Net 4.5 which
Background: Our web app uses the jquery.constrain.js plugin to handle data entry in some
Background I have a dimension table that has a single record for each day.
Background, Part 1 I have a form that collects both frequency and duration from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.