Skip to content

Instantly share code, notes, and snippets.

@ieure
Created October 5, 2009 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ieure/202322 to your computer and use it in GitHub Desktop.
Save ieure/202322 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import threading
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
engine = create_engine("mysql://testing:testing@10.0.0.1/testing",
pool_size=20, strategy="threadlocal")
Session = scoped_session(sessionmaker(bind=engine))
def query():
sess = Session()
res = sess.execute("SELECT SLEEP(1);")
print threading.currentThread().getName()
print " :%s" % (tuple(res),)
sess.close()
print "Spawning threads"
threads = [threading.Thread(target=query) for n in range(10)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print "%s complete" % thread.getName()
print "Terminating"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment