測試Python多執行緒與多程序

2021-08-15 03:45:31 字數 3535 閱讀 9383

#!/usr/bin/python

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

import time

import os

from threading import thread

from multiprocessing import process

def count(x, y):

# 使程式完成大量計算

for i in xrange(100000):

x += x

y += y

def write(filename):

f = open(filename, "w")

for i in xrange(100000):

f.write("testwrite\n")

f.close()

def read(filename):

f = open(filename, "r")

lines = f.readlines()

f.close()

def io(filename):

write(filename)

read(filename)

def testsingleprocess():

# cpu密集操作

t = time.time()

for i in xrange(10):

count(1, 1)

print("singleprocess cpu", time.time() - t)

# io密集操作

t = time.time()

for i in xrange(10):

io("test%d.txt"%i)

print("singleprocess io", time.time() - t)

def testmultithreading():

counts =

t = time.time()

for i in xrange(10):

thread = thread(target=count, args=(1,1))

thread.start()

e = counts.__len__()

while true:

for th in counts:

if not th.is_alive():

e -= 1

if e <= 0:

break

print("multithreading cpu",time.time() - t)

ios =

t = time.time()

for i in xrange(10):

thread = thread(target=io, args=("test%d.txt"%i,))

thread.start()

e = ios.__len__()

while true:

for th in ios:

if not th.is_alive():

e -= 1

if e <= 0:

break

print("multithreading io",time.time() - t)

def testmultiprocess():

counts =

t = time.time()

for i in xrange(10):

process = process(target=count, args=(1,1))

process.start()

e = counts.__len__()

while true:

for th in counts:

if not th.is_alive():

e -= 1

if e <= 0:

break

print("multiprocess cpu", time.time() - t)

ios =

t = time.time()

for i in xrange(10):

process = process(target=io, args=("test%d.txt"%i,))

process.start()

e = ios.__len__()

while true:

for th in ios:

if not th.is_alive():

e -= 1

if e <= 0:

break

print("multiprocess io", time.time() - t)

def cleartmpfile():

for i in xrange(10):

os.remove("test%d.txt"%i)

if __name__ == "__main__":

testsingleprocess()

time.sleep(2)

cleartmpfile()

time.sleep(2)

testmultithreading()

time.sleep(2)

cleartmpfile()

time.sleep(2)

testmultiprocess()

time.sleep(2)

cleartmpfile()

測試時的cpu執行 情況如下:

執行結果:

('singleprocess cpu', 41.20599985122681)

('singleprocess io', 0.3340001106262207)

('multithreading cpu', 41.062999963760376)

('multithreading io', 4.700000047683716)

('multiprocess cpu', 11.5239999294281)

('multiprocess io', 0.29200005531311035)

可以看出,只有多程序下,才能利用多核的優勢,將大量計算快速執行完畢

原因是:

python是執行在直譯器中的語言,有乙個全域性鎖(gil),在使用多執行緒(thread)的情況下,不能發揮多核的優勢。

而使用多程序(multiprocess),則可以發揮多核的優勢真正地提高效率。

另外,python還有基於協程的網路庫gevent

(github

)協程不同於執行緒的地方在於協程不是作業系統進行切換,而是由編碼進行切換,即由程式設計師控制的,這樣就沒有了執行緒的所謂安全問題。

python 多執行緒與多程序效率測試

目錄 在python中,計算密集型任務適用於多程序,io密集型任務適用於多執行緒 正常來講,多執行緒要比多程序效率更高,因為程序間的切換需要的資源和開銷更大,而執行緒相對更小,但是我們使用的python大多數的直譯器是cpython,眾所周知cpython有個gil鎖,導致執行計算密集型任務時多執行...

Python 多執行緒與多程序

前言 以前玩單機或者玩小資料集,都基本不用多執行緒或多程序都能基本滿足需求了 所以沒怎麼了解這方面的東西。但現在玩幾百萬甚至上千萬的資料,甚至集群等東西之後,就有必要學習多執行緒或多程序了。在python中首先要匯入相關的模組 import threading as td import multip...

python 多執行緒與多程序

程序與執行緒的區別 程序 應用程式的執行例項,每乙個執行中的程式就是乙個程序 執行緒 程序的組成部分,乙個程序可以擁有多個執行緒 在多執行緒中,會有乙個主線程來完成整個程序從開始到結束的全部操作,而其他的執行緒會在主線程的執行過程中被建立或退出。python景區賣票系統 多執行緒的應用 import...