扭曲:让code无阻塞 [英] Twisted: Making code non-blocking

查看:120
本文介绍了扭曲:让code无阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑如何用Python语言编写/异步扭曲code。假设(为了讨论)我露出一个函数向世界表明将采取数量,并返回真/假,如果是素/非黄金,所以它看起来依稀是这样的:

I'm a bit puzzled about how to write asynchronous code in python/twisted. Suppose (for arguments sake) I am exposing a function to the world that will take a number and return True/False if it is prime/non-prime, so it looks vaguely like this:


def IsPrime(numberin):
    for n in range(2,numberin):
        if numberin % n == 0: return(False)
    return(True)

(只是为了说明)。

(just to illustrate).

现在可以说有是需要基础上提交的值调用IsPrime一个网络服务器。这将需要很长的时间大 numberin

Now lets say there is a webserver which needs to call IsPrime based on a submitted value. This will take a long time for large numberin.

如果在另一个用户要求一个小数目的素性其间,有一个方法,以运行该两个函数调用异步使用反应器/ deferreds架构,使得短计算的结果得到的结果之前返回长计算?

If in the meantime another user asks for the primality of a small number, is there a way to run the two function calls asynchronously using the reactor/deferreds architecture so that the result of the short calc gets returned before the result of the long calc?

我知道如何做到这一点,如果使用isPrime功能从其他网络服务器到我的网络服务器会做延期GETPAGE,但是来了,如果它只是一个局部功能?

I understand how to do this if the IsPrime functionality came from some other webserver to which my webserver would do a deferred getPage, but what if it's just a local function?

即,可以扭曲两次调用IsPrime之间不知何故时间分享,或将需要一个新线程的显式调用?

i.e., can Twisted somehow time-share between the two calls to IsPrime, or would that require an explicit invocation of a new thread?

或者,将使用isPrime循环需要被分块成一系列较小的环,以便控制可以被传递回迅速将反应器

Or, would the IsPrime loop need to be chunked into a series of smaller loops so that control can be passed back to the reactor rapidly?

或者其他什么东西?

推荐答案

我觉得你目前的理解是基本正确。扭曲的仅仅是一个Python库和Python的code编写使用其正常执行你所期望的Python code到:如果只有一个线程(和一个单一的过程),那么只有一个事情发生在一个时间。通过双绞线提供几乎没有的API创建新的线程或进程,所以在事情发展的正常过程中的code顺序运行; isPrime 不能执行第二次,直到后,已完成执行的第一次。

I think your current understanding is basically correct. Twisted is just a Python library and the Python code you write to use it executes normally as you would expect Python code to: if you have only a single thread (and a single process), then only one thing happens at a time. Almost no APIs provided by Twisted create new threads or processes, so in the normal course of things your code runs sequentially; isPrime cannot execute a second time until after it has finished executing the first time.

不过只考虑单个线程(和一个单一的过程),所有的并发或扭曲的并行的来自于事实,而不是做阻塞网络I / O(和某些其他阻塞操作),扭曲提供了一种在非阻塞方式执行操作的工具。这使你的程序继续执行其他工作时,否则可能会一直停留无所事事等待阻塞I / O操作(如读取或写入到一个插座)来完成的。

Still considering just a single thread (and a single process), all of the "concurrency" or "parallelism" of Twisted comes from the fact that instead of doing blocking network I/O (and certain other blocking operations), Twisted provides tools for performing the operation in a non-blocking way. This lets your program continue on to perform other work when it might otherwise have been stuck doing nothing waiting for a blocking I/O operation (such as reading from or writing to a socket) to complete.

有可能使事情异步他们分裂成小块,并让事件处理这些块之间运行英寸这有时是一个有用的方法中,如果转换不使code太多更难理解和维护。 Twisted提供了帮手调度工作的这些块,<一个href=\"http://twistedmatrix.com/documents/current/api/twisted.internet.task.html#cooperate\"><$c$c>cooperate.有利的是使用该辅助,因为它可以使基于所有的工作的不同来源的调度决策,并确保有剩下的时间转移到服务的事件源而不显著额外的延迟(换言之,更多的作业添加到它,在更短的时间每个职业将获得,使反应堆能保持做的工作)。

It is possible to make things "asynchronous" by splitting them into small chunks and letting event handlers run in between these chunks. This is sometimes a useful approach, if the transformation doesn't make the code too much more difficult to understand and maintain. Twisted provides a helper for scheduling these chunks of work, cooperate. It is beneficial to use this helper since it can make scheduling decisions based on all of the different sources of work and ensure that there is time left over to service event sources without significant additional latency (in other words, the more jobs you add to it, the less time each job will get, so that the reactor can keep doing its job).

扭曲也没有处理线程和进程提供几个API。如果它不是显而易见如何突破作业成块,这些可能是有用的。您可以使用<一个href=\"http://twistedmatrix.com/documents/11.0.0/api/twisted.internet.threads.deferToThread.html\"><$c$c>deferToThread运行在一个线程池(线程安全的!)的功能。方便的是,这个API返回一个 递延 这将最终用函数的返回值火(或用失败如果函数抛出一个例外)。这些Deferreds看起来像任何其他的,并且尽可能使用它们来讲code,它也可以同样从像<一个打电话回来href=\"http://twistedmatrix.com/documents/current/api/twisted.web.client.html#getPage\"><$c$c>getPage - 这不使用额外的线程,只是非阻塞I / O和事件处理函数

Twisted does also provide several APIs for dealing with threads and processes. These can be useful if it is not obvious how to break a job into chunks. You can use deferToThread to run a (thread-safe!) function in a thread pool. Conveniently, this API returns a Deferred which will eventually fire with the return value of the function (or with a Failure if the function raises an exception). These Deferreds look like any other, and as far as the code using them is concerned, it could just as well come back from a call like getPage - a function that uses no extra threads, just non-blocking I/O and event handlers.

因为Python是不是非常适合于在单个进程中运行多个CPU密集型的线程,扭曲还提供用于启动和子进程通信的非阻塞API。您可以卸载计算,这样的流程,以充分利用额外的CPU或核心,而不必担心GIL放慢你失望的东西,无论是分块的战略或穿线方法提供。最低级别的API来处理这些过程是<一个href=\"http://twistedmatrix.com/documents/current/core/howto/process.html\"><$c$c>reactor.spawnProcess.还有安瓿,一个包,将管理一进程池为你提供一个模 deferToThread 的过程中,<一个href=\"http://bazaar.launchpad.net/~dialtone/ampoule/main/view/head:/ampoule/pool.py#L406\"><$c$c>deferToAMPProcess.

Since Python isn't ideally suited for running multiple CPU-bound threads in a single process, Twisted also provides a non-blocking API for launching and communicating with child processes. You can offload calculations to such processes to take advantage of additional CPUs or cores without worrying about the GIL slowing you down, something that neither the chunking strategy nor the threading approach offers. The lowest level API for dealing with such processes is reactor.spawnProcess. There is also Ampoule, a package which will manage a process pool for you and provides an analog to deferToThread for processes, deferToAMPProcess.

这篇关于扭曲:让code无阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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