1. 程式人生 > >python多線程和多進程

python多線程和多進程

.exe mysq date ide () mta tar 進程池 ase

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多線程和多進程