farcepest / MySQLdb1

MySQL database connector for Python (legacy version)
https://sourceforge.net/projects/mysql-python/
666 stars 318 forks source link

use multiprocessing to query in same mysqldb connnect , block? #113

Open rfyiamcool opened 8 years ago

rfyiamcool commented 8 years ago

I know many process not use same connect, Because this will be a problem .

BUT, run the under code , mysql request is block , then many process start to query sql at the same time , the sql is "select sleep(10)" , They are one by one.

I not found code abort lock/mutux in MySQLdb/mysql.c , Why there is no problem ? I think there will be problems with same connection fd in general . But pass my test, only block io, problems not arise . Where is the lock ?

import time
import multiprocessing
import MySQLdb

db = MySQLdb.connect("127.0.0.1","xiaorui.cc","xiaorui.cc","rui" )

def func(*args):
    while 1:
        cursor = db.cursor()
        cursor.execute("select sleep(10)")
        data = cursor.fetchall()
        print len(data)
        cursor.close()
        print time.time()
        time.sleep(0.1)

if __name__ == "__main__":
    task = []

    for i in range(20):
        p = multiprocessing.Process(target = func, args = (i,))
        p.start()
        task.append(p)
    for i in task:
        i.join()

result log, we found each request interval is ten seconds.

1
1464325514.82
1
1464325524.83
1
1464325534.83
1
1464325544.83
1
1464325554.83
1
1464325564.83
1
1464325574.83
1
1464325584.83
1
1464325594.83
1
1464325604.83
1
1464325614.83
1
1464325624.83

tcpdump log:

we found each request interval is ten seconds two.

13:07:04.827304 IP localhost.44281 > localhost.mysql: Flags [.], ack 525510411, win 513, options [nop,nop,TS val 2590846552 ecr 2590846552], length 0
    0x0000:  4508 0034 23ad 4000 4006 190d 7f00 0001  E..4#.@.@.......
    0x0010:  7f00 0001 acf9 0cea fc09 7cf9 1f52 a70b  ..........|..R..
    0x0020:  8010 0201 ebe9 0000 0101 080a 9a6d 2e58  .............m.X
    0x0030:  9a6d 2e58                                .m.X
13:07:04.928106 IP localhost.44281 > localhost.mysql: Flags [P.], seq 0:21, ack 1, win 513, options [nop,nop,TS val 2590846653 ecr 2590846552], length 21
    0x0000:  4508 0049 23ae 4000 4006 18f7 7f00 0001  E..I#.@.@.......
    0x0010:  7f00 0001 acf9 0cea fc09 7cf9 1f52 a70b  ..........|..R..
    0x0020:  8018 0201 fe3d 0000 0101 080a 9a6d 2ebd  .....=.......m..
    0x0030:  9a6d 2e58 1100 0000 0373 656c 6563 7420  .m.X.....select.
    0x0040:  736c 6565 7028 3130 29                   sleep(10)

13:07:14.827526 IP localhost.44281 > localhost.mysql: Flags [.], ack 65, win 513, options [nop,nop,TS val 2590856553 ecr 2590856552], length 0
    0x0000:  4508 0034 23af 4000 4006 190b 7f00 0001  E..4#.@.@.......
    0x0010:  7f00 0001 acf9 0cea fc09 7d0e 1f52 a74b  ..........}..R.K
    0x0020:  8010 0201 9d73 0000 0101 080a 9a6d 5569  .....s.......mUi
    0x0030:  9a6d 5568                                .mUh
13:07:14.927960 IP localhost.44281 > localhost.mysql: Flags [P.], seq 21:42, ack 65, win 513, options [nop,nop,TS val 2590856653 ecr 2590856552], length 21
    0x0000:  4508 0049 23b0 4000 4006 18f5 7f00 0001  E..I#.@.@.......
    0x0010:  7f00 0001 acf9 0cea fc09 7d0e 1f52 a74b  ..........}..R.K
    0x0020:  8018 0201 fe3d 0000 0101 080a 9a6d 55cd  .....=.......mU.
    0x0030:  9a6d 5568 1100 0000 0373 656c 6563 7420  .mUh.....select.
    0x0040:  736c 6565 7028 3130 29                   sleep(10)

Thanks You .