Python 執行緒池

2021-09-21 06:58:27 字數 3275 閱讀 3570

#! /usr/bin/env python

# -*- coding: utf-8 -*-

import threadpool

import time

def sayhello(a):

print("hello: "+a)

time.sleep(2)

def main():

name_list = ['liu', 'hai', 'don']

start = time.time()

# 5是執行緒池中線程的個數

pool = threadpool.threadpool(5)

requests = threadpool.makerequests(sayhello, name_list)

for req in requests:

pool.putrequest(req)

#等待所有任務處理完成,則返回,如果沒有處理完,則一直阻塞

pool.wait()

end = time.time()

print("time: "+str(end-start))

start1 = time.time()

for each in name_list:

sayhello(each)

end1 = time.time()

print("time1: "+str(end1-start1))

if __name__ == '__main__':

main()

# 結果(大約節省4秒時間):

# hello: liu

# hello: hai

# hello: don

# time: 2.00300002098

# hello: liu

# hello: hai

# hello: don

# time1: 6.00100016594

# -*- coding: utf-8 -*-

import threadpool

import time

def hello(m, n, o):

print (m, n, o)

print "m = %s, n = %s, o = %s" % (m, n, o)

time.sleep(2)

if __name__ == '__main__':

# 主要找到了兩種方法,一種是將引數構造成list進行傳入;還有一種是將引數構造成dict進行傳入。

# 方法1

lst_vars_1 = ['1', '2', '3']

lst_vars_2 = ['4', '5', '6']

lst_vars_3 = ['7', '8', '9']

func_var = [(lst_vars_1, none), (lst_vars_2, none), (lst_vars_3, none)]

# 方法2

dict_vars_1 =

dict_vars_2 =

dict_vars_3 =

func_var = [(none, dict_vars_1), (none, dict_vars_2), (none, dict_vars_3)]

# 2是執行緒池中線程的個數

pool = threadpool.threadpool(2)

# 通過傳入乙個引數組來實現多執行緒,並且它的多執行緒是有序的,順序與引數組中的引數順序保持一致。

requests = threadpool.makerequests(hello, func_var)

[pool.putrequest(req) for req in requests]

pool.wait()

# pool.poll()

# 結果:

# ('1', '2', '3')

# m = 1, n = 2, o = 3

# ('4', '5', '6')

# m = 4, n = 5, o = 6

# 延遲兩秒之後,列印下面的資訊

# ('7', '8', '9')

# m = 7, n = 8, o = 9

# 參考:

# 參考:

#! /usr/bin/env python

# -*- coding: utf-8 -*-

from concurrent.futures import threadpoolexecutor

import time

def sayhello(a):

print("hello: "+a)

time.sleep(2)

def main():

seed = ["a", "b", "c"]

start1 = time.time()

for each in seed:

sayhello(each)

end1 = time.time()

print("time1: "+str(end1-start1))

start2 = time.time()

with threadpoolexecutor(3) as executor:

for each in seed:

executor.submit(sayhello, each)

end2 = time.time()

print("time2: "+str(end2-start2))

start3 = time.time()

with threadpoolexecutor(3) as executor1:

executor1.map(sayhello, seed)

end3 = time.time()

print("time3: "+str(end3-start3))

if __name__ == '__main__':

main()

# 結果:

# hello: a

# hello: b

# hello: c

# time1: 6.00099992752

# hello: a

# hello: b

# hello: c

# time2: 2.02200007439

# hello: a

# hello: b

# hello: c

# time3: 2.01999998093

# 參考:

# 參考:

python 執行緒池 Python的執行緒池

usr bin env python coding utf 8 concurrent 用於執行緒池和程序池程式設計而且更加容易,在python3.2中才有。import sys from concurrent.futures import threadpoolexecutor,as complete...

python 執行緒池 python執行緒池原始碼解析

本篇主要講下threadpoolexecutor的實現。由於業務量不大,且一直使用框架進行程式設計,對執行緒的理解一直很模糊,基本處於不想阻塞程式執行,起乙個執行緒啟動任務的階段。總感覺自己好像會執行緒一樣,實則一直處於一種懵懂狀態,通過一段時間檢視一些別人寫的原始碼,終於有所悟,也記錄下自己的學習...

python執行緒池

import time threadpool為執行緒池模組 import threadpool deftest str print str time.sleep 2 if name main starttime time.time 建立執行緒池,最多建立的執行緒數為10 pool threadpoo...