Hi,

mod_wsgi is multi-threaded.  This means that you could be possibly sharing
a connection across multiple concurrent requests.

Look up the threading.local in the python docs.  This can give you a class
instance or object that has thread-specific properties:


*class myapp(threading.local):*
*  _db = None*

*  def __init__(self):*
*    # Called once per thread*

*  @property*
*  def db(self):*
*    if self._db is None or self._db.status == 'closed':*
*      # connect to db and store it in self._db*
*    return self._db*

*app = myapp()   #app is a singleton object that lives for the life of the
process.*

---
Now from your code anywhere:

>From anywhere in your code, you can say:

*from yourpackage import app*

*app.db.cursor() *
*...*

And it will never be shared across threads.

There are many ways to use threading.local, but this is a proven one.

Jason


On Wed, Nov 12, 2014 at 11:36 AM, Petr Šimek <[email protected]> wrote:

> Hello,
>
> Inside of Flask Python application I access MSSQL server like this:
>
> cnxn = pyodbc.connect(appnmp.app.config['LYNCDB_CONNECTION_STRING'])
> cursor = cnxn.cursor()
> cursor.execute(...
> ... cursor.fetchall()
> cursor.close()
> cnxn.close()
>
> I have no problem when running application like this:
>
> #appnmp creation:
> from flask import Flask
> app = None
> def init(_name):
> # Call once only!
> global app
> app = Flask(_name)
>
> #runserver:
> import appnmp
> import os
> if __name__ == '__main__':
> appnmp.app.run(port=int(os.getenv('FLASK_PORT', 5000)))
>
> When I run the application as mod_wsgi inside the Apache, then the MSSQL
> connection seems to be shared among different requests. When I execute 2
> requests in parallel, then none of them get's data from database. The only
> help is to restart Apache. (And all is working fine including parallel
> requests when application runs outside apache with runserver as shown above)
>
> This is the apache config file:
>
> <VirtualHost *:443>
>     ServerName czchols2643.prg-dc.dhl.com
>     ServerAlias nmp.dhl.com
>     ServerAdmin [email protected]
>
>     LogLevel info
>     ErrorLog /data/log/apache_error.log
>     TransferLog /data/log/apache_access.log
>
>     DocumentRoot /appl/nmp/app/appnmp/static
>
>     <Directory "/appl/nmp/app/appnmp/static">
>         Options FollowSymLinks
>         AllowOverride None
>         Order allow,deny
>         Allow from all
>
>         AddOutputFilterByType DEFLATE application/javascript text/css
> text/plain
>     </Directory>
>
>     WSGIDaemonProcess appnmp user=apache group=apache processes=1
>     WSGIScriptAlias / /appl/nmp/app/nmp.wsgi
>
>     <Directory /appl/nmp/app>
>         WSGIProcessGroup appnmp
>         WSGIApplicationGroup %{GLOBAL}
>         Order deny,allow
>         Allow from all
>     </Directory>
>
>     <IfModule mod_headers.c>
>         Header unset Server
>         Header unset X-Powered-By
>     </IfModule>
>
>     SSLEngine on
>     SSLCipherSuite
> ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
>     SSLCertificateFile "/appl/nmp/home/nmp.dhl.com.crt"
>     SSLCertificateKeyFile "/appl/nmp/home/nmp.dhl.com.key"
>
> </VirtualHost>
>
> Any idea please? It seems to me the db-connection object is somehow shared
> and different requests write to the same place in memory.
>
>  --
> You received this message because you are subscribed to the Google Groups
> "modwsgi" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/modwsgi.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

Reply via email to