通过并行线程提高Python执行速度

0 投票
4 回答
3443 浏览
提问于 2025-04-17 07:58

假设我有这段示例代码:

x = foo1(something1)
y = foo2(something2)

z = max(x, y)

我想通过使用线程来提高这段代码的执行速度(希望这样能有所帮助,对吧?)。我想尽量保持简单,所以我基本上想做的就是创建两个线程,让它们同时工作,分别计算 foo1foo2

我在阅读关于线程的内容,但觉得有点复杂,我不想花太多时间在这上面,只是想做这么简单的事情。

4 个回答

0

首先请查看文档,了解下面的代码:这里

import threading

def foo1(x=0):
    return pow(x, 2)

def foo2(y=0):
    return pow(y, 3)

thread1 = threading.Thread(target=foo1, args=(3))
thread2 = threading.Thread(target=foo2, args=(2))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
0

你可以使用Python的thread模块或者新的Threading模块,不过使用thread模块的语法更简单。

#!/usr/bin/python

import thread
import time

# Define a function for the thread
def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows
try:
   thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
   print "Error: unable to start thread"

while 1:
   pass

这段代码会输出:

Thread-1: Thu Jan 22 15:42:17 2009
Thread-1: Thu Jan 22 15:42:19 2009
Thread-2: Thu Jan 22 15:42:19 2009
Thread-1: Thu Jan 22 15:42:21 2009
Thread-2: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:25 2009
Thread-2: Thu Jan 22 15:42:27 2009
Thread-2: Thu Jan 22 15:42:31 2009
Thread-2: Thu Jan 22 15:42:35 2009

想了解更多,可以查看这里,Python - 多线程编程

8

假设 foo1foo2 是计算密集型的,也就是说它们主要在用CPU进行大量计算,那么使用多线程并不会让执行速度变快……实际上,通常会让速度变得更慢……想了解更多,可以看看 David Beazley在PyCon2010的演讲,讲的是全局解释器锁 / Pycon2010 GIL的幻灯片。这场演讲信息量很大,我强烈推荐给任何想要在CPU核心之间分配负载的人。

提高性能的最好方法是使用 多进程模块

假设 foo1()foo2() 之间不需要共享状态,可以这样做来提升执行性能……

from multiprocessing import Process, Queue
import time

def foo1(queue, arg1):
    # Measure execution time and return the total time in the queue
    print "Got arg1=%s" % arg1
    start = time.time()
    while (arg1 > 0):
        arg1 = arg1 - 1
        time.sleep(0.01)
    # return the output of the call through the Queue
    queue.put(time.time() - start)

def foo2(queue, arg1):
    foo1(queue, 2*arg1)

_start = time.time()
my_q1 = Queue()
my_q2 = Queue()

# The equivalent of x = foo1(50) in OP's code
p1 = Process(target=foo1, args=[my_q1, 50])
# The equivalent of y = foo2(50) in OP's code
p2 = Process(target=foo2, args=[my_q2, 50])

p1.start(); p2.start()
p1.join(); p2.join()
# Get return values from each Queue
x = my_q1.get()
y = my_q2.get()

print "RESULT", x, y
print "TOTAL EXECUTION TIME", (time.time() - _start)

在我的机器上,这样的结果是:

mpenning@mpenning-T61:~$ python test.py 
Got arg1=100
Got arg1=50
RESULT 0.50578212738 1.01011300087
TOTAL EXECUTION TIME 1.02570295334
mpenning@mpenning-T61:~$ 

撰写回答