Skip to content

Instantly share code, notes, and snippets.

@cutewalker
Created January 7, 2015 01:21

Revisions

  1. cutewalker created this gist Jan 7, 2015.
    45 changes: 45 additions & 0 deletions mysqldb_threading.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # -*- coding: utf-8 -*-

    import MySQLdb
    import threading
    import time

    import sqlalchemy.pool
    MySQLdb = sqlalchemy.pool.manage(MySQLdb)

    #db = MySQLdb.connect("localhost", "root", "", "miztest")
    #cursor = db.cursor()

    class Runner(threading.Thread):
    def __init__(self, name, counts):
    super(Runner, self).__init__(name=name)
    self.counts = counts
    #self.db = MySQLdb.connect("localhost", "root", "", "miztest")
    #self.cursor = self.db.cursor()

    def run(self):
    for x in xrange(self.counts):
    self._do_db_thing(x)
    time.sleep(0.1)

    def _do_db_thing(self, x):
    sql = "insert into foo values(%s, %s)" % (self.name, x)
    #self.cursor.execute(sql)
    #self.db.commit()
    db = MySQLdb.connect("localhost", "root", "", "miztest")
    cursor = db.cursor()
    cursor.execute(sql)
    db.commit()
    print id(db)

    if __name__ == '__main__':
    runners = []
    for x in range(1, 6):
    runners.append(Runner(x, 50))

    for r in runners:
    r.start()

    for r in runners:
    r.join()