码迷,mamicode.com
首页 > 编程语言 > 详细

python多线程和多进程

时间:2018-09-20 22:54:24      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:准备   threading   --   follow   mysqldb   process   one   csdn   ima   

Python多线程存取MySQL数据

为什么在Python里推荐使用多进程而不是多线程?

廖雪峰关于多进程

python进程池剖析(二)

示例说话

准备数据库,数据表

# 新建数据库
create database mxshop;

# 新建测试表
create table table_iin (id int primary key auto_increment, `in` int, time datetime);

# 授权用户
grant all on mxshop.* to mxshop_user@localhost identified by ‘mxshop_pass‘;

insert插入数据 -- 多线程插入

import MySQLdb
import datetime
import time
import threading

def insert(io):
        time_now = datetime.datetime.now()
        print io,time_now
        conn = MySQLdb.connect(user = "mxshop_user", passwd = "mxshop_pass", host = "localhost", db = "mxshop")
        cur = conn.cursor()
        sql = "insert into table_in (`in`, `time`) values (‘%s‘,‘%s‘);"
        cur.execute(sql%(io,time_now))
        #sql = ‘show databases;‘
        #print cur.execute(sql)
        cur.close()
        conn.commit()
        time_end = datetime.datetime.now()
        print ‘\033[41mTask     %s runs %s seconds.\033[0m‘ % (io, (time_end - time_now))
        # time.sleep(2)
print ‘Parent process begin‘
t_res = []
for i in range(1,313):
    t = threading.Thread(target=insert, args=(i,))
    t.start()
    t_res.append(t)

for r in t_res:
    r.join()
print ‘\033[42mWaiting for all subpro done\033[0m‘
print ‘all done‘

update更新数据 -- 多进程更新

import MySQLdb
import datetime
import time
import threading
from multiprocessing import Pool

def update_sql(io):
        time_now = datetime.datetime.now()
        print io,time_now
        conn = MySQLdb.connect(user = "mxshop_user", passwd = "mxshop_pass", host = "localhost", db = "mxshop")
        cur = conn.cursor()
        sql = "update table_in set `time`=‘%s‘ where `in`=‘%s‘;"
        cur.execute(sql%(time_now,io))
        #sql = ‘show databases;‘
        #print cur.execute(sql)
        cur.close()
        conn.commit()
        time_end = datetime.datetime.now()
        print ‘\033[41mTask     %s runs %s seconds.\033[0m‘ % (io, (time_end - time_now))
        # time.sleep(2)
print ‘Parent process begin‘
i = 1
# 多次循环 update,注意 Pool()应在的位置
while i < 5:
    p = Pool()
    for n in range(1,313):
        p.apply_async(update_sql, args=(n,))
    #    if n == 5:
    #        break
    print ‘\033[42mWaiting for all subpro done\033[0m‘
    p.close()
    p.join()
    print ‘\033[32m %s all done\033[0m‘ %i
    i += 1

多线程适用IO密集型的操作,不适合CPU密集型,为什么呢?

python多线程和多进程

标签:准备   threading   --   follow   mysqldb   process   one   csdn   ima   

原文地址:http://blog.51cto.com/11114389/2177872

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!