asynchronous - Designing an async API in Python -


(Note: This question is strictly about the API's design , not how applicable That means, I only look at my API's client that I only care about what I have to do to get this work done.)

In simple words: I want to know the established pattern I - if any - for (alias promises, aka deferred, aka work-name differ on the basis of structure) In Python. The following is a more detailed description.

Consider a simple Python API like this:

  def read_line (): ... s = read_line () print (s)  < / Pre> 

This is a narrow version - if a line is not yet available it will be blocked. Let's say, now, I want to provide a compatible asynchronous (non-blocking) version that allows to enter a callback after the operation is completed. A simple version might look like this:

  def read_line_async (callback): ... read_line_async (lambda s: print (s))   

Now, in other languages ​​and frameworks, there are often existing compulsory or at least well-established patterns for such APIs. For example, prior to version 4. In the .NET, one will typically provide a pair of BeginReadLine / andrideline methods, and the stock IAsyncResult interface Uses a System.Threading.Tasks in .NET 4+ to register the callback and pass the resulting values, so that all the task combination operators ( WhenAll etc.) and the C # 5.0 Async feature.

For another example in JavaScript, there is nothing to cover it in the standard library, but jQuery has made the "deferred promise" interface popular now. So if I want to type async readLine in JS, then I name it as readLineAsync , and implement the then method at the returned value Give it

What, if any, is the pattern established in Python Land? While searching for the standard library, I provide asynchronous APIs to many modules, but there is no consistent pattern between them, and nothing like standardized protocol for "work" or "promises". Maybe some third-party libraries can be obtained from?

I also saw the twisted (referred to in this context) category, but it seems that a general-promotion promise API, nor is it suited to the specific needs of this library. It does not seem like anything that I can easily clone the clone of an interface (without dependence), as if our promises have to be interoperated well if the client uses their libraries with both Another popular library or framework for which there is a clearly designed API, can I copy (and simultaneously) without direct dependence? ?

OK, so I've found, in which there is future class. As much as I can see it, I can not use it because the proper examples are only made by execute , and it is a class of converting the existing synchronous API into an asynchronization. While synchronizing call is taking to the background thread, I have the future Happy I can repeat the exact way provided by objects - what they expect, they match very closely, i.e. add strength, cancellation, and callback of query (blocked) for the result.

Is this sound like a reasonable perspective? Is this possible, with the proposal to add an abstract base class to the Python Storied Library for a general "future" concept, as if they have Python archives.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -