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

标签 python python-3.x multithreading concurrency python-multithreading

我正在尝试理解 Python 中的线程。我查看了文档和示例,但坦率地说,许多示例过于复杂,我无法理解它们。

您如何清楚地显示为多线程划分的任务?

最佳答案

自从 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/2846653/

相关文章:

python - 如何在python中填充矩阵

python - 重复列值以适合列名称数组

python - 在 py.test session 开始之前运行一个文件,没有 __init__

python - 从 SQL 表(Python)中选择数据时如何去掉括号?

c++ - Android JNI——线程同步

java - SwingWorker publish()/process() 就像 done()

python - 云间距

python - ObjectListView 使用逗号对整数进行排序和格式化?

python-3.x - 如何在 xlsxwriter 中格式化空单元格的范围

java - 如何在 Selenium Grid 上对两个单例浏览器进行多线程处理?