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 8732915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:32:04+00:00 2026-06-13T09:32:04+00:00

My stack is Django 1.3(Python 2.7) + Apache + mod_wsgi Apache spawns a new

  • 0

My stack is Django 1.3(Python 2.7) + Apache + mod_wsgi

Apache spawns a new process per request… Sometimes in those requests I have to Publish a message to RabbitMQ about some heavy task to be done later (like notifying someone’s followers about a cat photo they just posted).

Publishing involves making connection to RabbitMQ, which is expensive. (see this SO question: RabbitMQ create connection is an expensive operation)

I am also sadly aware of Python’s incapability to share memory across processes… sigh 🙁

How in this case, do I prevent the overhead of creating and closing a RabbitMQ connection per apache request?

For Clarity, Using RabbitMQ + pika here is my failed attempt at sharing the connection between processes:

def test_view(request):
    """a django view publishing to RabbitMQ"""
    connection = Connection.get_shared_connection()
    channel = connection.channel() 
    channel.queue_declare(queue='test_queue', auto_delete=False, durable=True)
    channel.basic_publish(exchange='', routing_key='test_queue', body='Hello Rabbit!', 
                                properties=pika.BasicProperties(delivery_mode=2))
    print 'SENT:', body

    return HttpResponse("published")


class Connection(object):
    """failed attempt to create a singleton"""
    _connection = None

    @staticmethod
    def get_shared_connection(): # return a pika connection
        if not Connection._connection:
            print 'NEW CONNECTION'  # prints NEW CONNECTION with every request
            Connection._connection = pika.BlockingConnection(pika.ConnectionParameters(
                                                                           host='localhost'))
        return Connection._connection

In server setting (Django + Apache + mod_wsgi) 'NEW CONNECTION' is printed with every request made to test_view

It seems like an issue anybody would run into using Django with RabbitMQ. There has to be a way…

Thanks… any help is appreciated

  • 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-13T09:32:05+00:00Added an answer on June 13, 2026 at 9:32 am

    Apache does NOT spawn a new process per request.

    You might be getting deceived into thinking it is because in embedded mode it can be a multiprocess web server, thus different requests could be getting handled in a separate process.

    I would have suggested you use mod_wsgi daemon mode and use a single multithreaded process, but unfortunately your code is not thread safe as is because you don’t protect creation of your global socket connection from multiple threads at the same time. But then, that alone will not help because if multiple threads try and use that one connection at the same time, it will likely cause problems. So you really need to have a connection object per thread.

    I suggest you read the following to get some background on different process/thread models of Apache/mod_wsgi.

    • http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html
    • http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
    • http://blog.dscpl.com.au/2009/03/python-interpreter-is-not-created-for.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My web stack is django/python + postgresql + linux + apache. I use fabric
I have python/django app on Heroku (Cedar stack) and would like to make it
Here's the setup: Django (1.2) app on mod_wsgi that imports ctypes Python 2.6.5 Apache
This is a apache/mod_wsgi/virtualenv/django stack. In the virtualenv site-packages dir I've got a virtualenv_path_extensions.pth
I have the following stack trace when trying to access the django admin default
So I installed Bitnami Django stack, hoping as proclaimed 'ready-to-run' versions of python and
I'm using Python/Django on Heroku (Cedar Stack) and I've got a management command that
I am new to django development (I was working with MS and Oracle/Java stack).
I am new to both django and python but I am beginning to get
I have: Python 2.6 Django 1.1.1 I downloaded Django-cms from git://github.com/digi604/django-cms-2.0.git I passed south

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.