使用 Twisted 实现简单的非网络并发
我在 python 中使用 Twisted 进行简单并发时遇到问题。问题是 - 我不知道该怎么做,并且所有在线资源都是关于 Twisted 网络功能的。所以我向 SO 大师寻求一些指导。
使用Python 2.5。
我的问题的简化版本运行如下:
- 一堆科学数据
- 一个咀嚼数据并创建输出的函数
- ??? <这里输入并发,它从 1 获取数据块并将其提供给 2
- 来自 3 的输出被连接并存储
我的猜测是 Twisted < code>reactor 可以完成第三项工作。但如何呢?
非常感谢您的任何帮助和建议。
upd1:
简单的示例代码。不知道reactor如何处理进程,所以我给了它虚函数:
datum = 'abcdefg'
def dataServer(data):
for char in data:
yield chara
def dataWorker(chara):
return ord(chara)
r = reactor()
NUMBER_OF_PROCESSES_AV = 4
serv = dataserver(datum)
id = 0
result = array(len(datum))
while r.working():
if NUMBER_OF_PROCESSES_AV > 0:
r.addTask(dataWorker(serv.next(), id)
NUMBER_OF_PROCESSES_AV -= 1
id += 1
for pr, id in r.finishedProcesses():
result[id] = pr
I have a problem with using Twisted for simple concurrency in python. The problem is - I don't know how to do it and all online resources are about Twisted networking abilities. So I am turning to SO-gurus for some guidance.
Python 2.5 is used.
Simplified version of my problem runs as follows:
- A bunch of scientific data
- A function that munches on the data and creates output
- ??? < here enters concurrency, it takes chunks of data from 1 and feeds it to 2
- Output from 3 is joined and stored
My guess is that Twisted reactor
can do the number three job. But how?
Thanks a lot for any help and suggestions.
upd1:
Simple example code. No idea how reactor deals with processes, so I have given it imaginary functions:
datum = 'abcdefg'
def dataServer(data):
for char in data:
yield chara
def dataWorker(chara):
return ord(chara)
r = reactor()
NUMBER_OF_PROCESSES_AV = 4
serv = dataserver(datum)
id = 0
result = array(len(datum))
while r.working():
if NUMBER_OF_PROCESSES_AV > 0:
r.addTask(dataWorker(serv.next(), id)
NUMBER_OF_PROCESSES_AV -= 1
id += 1
for pr, id in r.finishedProcesses():
result[id] = pr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 Jean-Paul 所说,Twisted 非常适合协调多个流程。但是,除非您需要使用 Twisted,并且只需要一个分布式处理池,否则可能有更适合的工具。
我能想到的一个没有被提及的是 celery。 Celery 是一个分布式任务队列 - 您可以设置运行 DB、Redis 或 RabbitMQ(您可以从许多免费软件选项中进行选择)的任务队列,并编写许多计算任务。这些可以是任意科学计算类型的任务。任务可以产生子任务(实现上面提到的“加入”步骤)。然后,您可以根据需要启动尽可能多的工作人员并进行计算。
我是 Twisted 和 Celery 的重度用户,所以无论如何,这两个选项都不错。
As Jean-Paul said, Twisted is great for coordinating multiple processes. However, unless you need to use Twisted, and simply need a distributed processing pool, there are possibly better suited tools out there.
One I can think of which hasn't been mentioned is celery. Celery is a distributed task queue - you set up a queue of tasks running a DB, Redis or RabbitMQ (you can choose from a number of free software options), and write a number of compute tasks. These can be arbitrary scientific computing type tasks. Tasks can spawn subtasks (implementing your "joining" step you mention above). You then start as many workers as you need and compute away.
I'm a heavy user of Twisted and Celery, so in any case, both options are good.
要实际同时计算事物,您可能需要使用多个 Python 进程。单个 Python 进程可以交错计算,但它不会并行执行它们(有一些例外)。
Twisted 是协调这些多个流程并收集其结果的好方法。 Ampoule 是一个致力于解决这一任务的库。您可以在其 Launchpad 页面上找到有关 Ampoule 的更多信息:https://launchpad.net/ampoule。
To actually compute things concurrently, you'll probably need to employ multiple Python processes. A single Python process can interleave calculations, but it won't execute them in parallel (with a few exceptions).
Twisted is a good way to coordinate these multiple processes and collect their results. One library oriented towards solving this task is Ampoule. You can find more information about Ampoule on its Launchpad page: https://launchpad.net/ampoule.
您到底需要 Twisted 吗?
根据您对问题的描述,我想说 multiprocessing 符合要求。创建许多
Process
对象,这些对象被赋予对单个Queue
实例的引用。让他们开始工作并将结果放入队列
。只需使用阻塞get()
即可读取结果。Do you need Twisted at all?
From your description of the problem I'd say that multiprocessing would fit the bill. Create a number of
Process
objects that are given a reference to a singleQueue
instance. Get them to start their work and put their results on theQueue
. Just use blockingget()
s to read the results.在我看来,您似乎误解了 Twisted 运作的基本原理。我建议您尝试一下 Twisted Intro < a href="http://krondo.com" rel="nofollow noreferrer">戴夫·佩蒂科拉斯。它对我有很大帮助,我已经使用 Twisted 多年了!
提示:Twisted 中的所有内容都依赖于 reactor!
(来源:krondo.com)
It seems to me that you are misunderstanding the fundamentals of how Twisted operates. I recommend you give the Twisted Intro a shot by Dave Peticolas. It has been a great help to me, and I've been using Twisted for years!
HINT: Everything in Twisted relies on the reactor!
(source: krondo.com)