python - 如何在 Python 中使用线程?

标签 python multithreading concurrency python-multithreading

我想要一个清晰的示例,显示任务被划分到多个线程中。

最佳答案

自从 2010 年提出这个问题以来,如何使用 Python 使用 map 进行简单的多线程处理已经有了真正的简化。 pool

下面的代码来自您绝对应该查看的文章/博客文章(无隶属关系)- Parallelism in one line: A Better Model for Day to Day Threading Tasks 。我将在下面进行总结 - 它最终只是几行代码:

from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(4)
results = pool.map(my_function, my_array)

哪个是多线程版本:

results = []
for item in my_array:
    results.append(my_function(item))
<小时/>

描述

Map is a cool little function, and the key to easily injecting parallelism into your Python code. For those unfamiliar, map is something lifted from functional languages like Lisp. It is a function which maps another function over a sequence.

Map handles the iteration over the sequence for us, applies the function, and stores all of the results in a handy list at the end.

Enter image description here

<小时/>

实现

Parallel versions of the map function are provided by two libraries:multiprocessing, and also its little known, but equally fantastic step child:multiprocessing.dummy.

multiprocessing.dummy 与多处理模块完全相同,but uses threads instead ( an important distinction - 对 CPU 密集型任务使用多个进程;对 I/O(以及期间)使用线程):

multiprocessing.dummy replicates the API of multiprocessing, but is no more than a wrapper around the threading module.

import urllib2
from multiprocessing.dummy import Pool as ThreadPool

urls = [
  'http://www.python.org',
  'http://www.python.org/about/',
  'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',
  'http://www.python.org/doc/',
  'http://www.python.org/download/',
  'http://www.python.org/getit/',
  'http://www.python.org/community/',
  'https://wiki.python.org/moin/',
]

# Make the Pool of workers
pool = ThreadPool(4)

# Open the URLs in their own threads
# and return the results
results = pool.map(urllib2.urlopen, urls)

# Close the pool and wait for the work to finish
pool.close()
pool.join()

以及计时结果:

Single thread:   14.4 seconds
       4 Pool:   3.1 seconds
       8 Pool:   1.4 seconds
      13 Pool:   1.3 seconds
<小时/>

传递多个参数(工作方式如下 only in Python 3.3 and later ):

传递多个数组:

results = pool.starmap(function, zip(list_a, list_b))

或者传递一个常量和一个数组:

results = pool.starmap(function, zip(itertools.repeat(constant), list_a))

如果您使用的是早期版本的 Python,则可以通过 this workaround 传递多个参数)。

(感谢 user136036 的有用评论。)

关于python - 如何在 Python 中使用线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56701592/

相关文章:

python - PyGraphviz将Networkx图形标签绘制为方框

python - 使用 Flask 转换上传的 CSV 文件上传

java - 了解 Java 固定线程池

java - 多线程 Socket 通讯 Client/Server

java - 如何安全地将线程生命周期绑定(bind)在一起,涉及 SQLite

go - 同时从两个 channel 消费会导致 goroutine 占用我的 RAM

java - 无法使用 Thread.start() 启动 JavaFX 任务

python - 通过 brenda-web 界面将参数传递给 python blender 脚本,如何解析参数?

没有互斥量的条件等待

python - 使用 tf.data API 和样本权重进行训练