如何在Python中使用线程? [英] How can I use threading in Python?

查看:93
本文介绍了如何在Python中使用线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Python中的线程.我看过文档和示例,但是坦率地说,许多示例过于复杂,我难以理解它们.

I am trying to understand threading in Python. I've looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I'm having trouble understanding them.

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

How do you clearly show tasks being divided for multi-threading?

推荐答案

自2010年提出此问题以来,如何使用

Since this question was asked in 2010, there has been real simplification in how to do simple multithreading with Python with map and pool.

下面的代码来自您一定要检出的文章/博客文章(无隶属关系)- 并行处理:一行更好的日常线程任务模型 .我将在下面进行总结-最终仅是几行代码:

The code below comes from an article/blog post that you should definitely check out (no affiliation) - Parallelism in one line: A Better Model for Day to Day Threading Tasks. I'll summarize below - it ends up being just a few lines of code:

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

哪个是以下版本的多线程版本:

Which is the multithreaded version of:

results = []
for item in my_array:
    results.append(my_function(item))


说明

Map是一个很棒的小功能,它是轻松将并行性注入Python代码的关键.对于那些不熟悉的人来说,地图是从Lisp之类的功能语言中提炼出来的.它是将另一个功能映射到序列上的功能.

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为我们处理序列的迭代,应用该函数,并将所有结果存储在最后的方便列表中.

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.

实施

map函数的并行版本由以下两个库提供:多处理,以及鲜为人知但同样出色的继子:multiprocessing.dummy.

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与多处理模块完全相同,但使用线程代替( 重要区别 -将多个进程用于CPU密集型任务;(在I/O期间(以及在此期间)线程):

multiprocessing.dummy is exactly the same as multiprocessing module, but uses threads instead (an important distinction - use multiple processes for CPU-intensive tasks; threads for (and during) I/O):

multiprocessing.dummy复制了多处理的API,但仅不过是线程模块的包装器.

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


传递多个参数(这样的操作仅在Python 3.3和更高版本中使用):


Passing multiple arguments (works like this only in Python 3.3 and later):

要传递多个数组:

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

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

Or to pass a constant and an array:

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

如果您使用的是Python的早期版本,则可以通过

If you are using an earlier version of Python, you can pass multiple arguments via this workaround).

(感谢 user136036 的帮助.)

这篇关于如何在Python中使用线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆