郁金香/ ASYNCIO:为什么不能所有的呼叫是异步,并指定时,事情应该是同步的? [英] Tulip/asyncIO: why not all calls be async and specify when things should be synchronous?

查看:170
本文介绍了郁金香/ ASYNCIO:为什么不能所有的呼叫是异步,并指定时,事情应该是同步的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我去SF Python的聚会时,圭多谈到了解郁金香,未来ASYNCIO库在Python异步操作。

I went to the SF Python meetup when Guido talked about Tulip, the future asyncIO library for asynchronous operations in Python.

该带走的是,如果你想要的东西被异步运行,你可以从收益+ EX pression 使用和一些装饰的指定那之后产量随之而来的呼吁,也应异步执行。关于它的好处是,你可以读取该功能正常的声明(就好像它是同步的),它的行为就好像它是同步相对于该函数的执行(返回值和错误/异常传播和处理)。

The take away is that if you want something to be run asynchronously you can use the "yield from" + expression and a couple of decorators to specify that the call to what comes after yield from should be executed asynchronously. The nice thing about it is that you can read the statements in that function normally (as if it was synchronous) and it will behave as if it was synchronous with respect to the execution of that function (return values and error/exception propagation and handling).

我的问题是:为什么不能有相反的行为,即拥有所有的函数调用是默认异步(无需从收益),并有不同的明确的语法当你想同步执行的东西吗?

My question is: why not have the opposite behavior, namely, have all function calls be by default async (and without the yield from) and have a different explicit syntax when you want to execute something synchronously?

(除了需要另一个关键字/语法规范)

(besides the need for another keyword/syntax spec)

推荐答案

真正的答案是,圭多的喜欢的事实,即异步屈服点是明确的协同程序,因为如果你不知道,一个电话可以产生,那么这是一个邀请并发性问题 - 就像线程。但是,如果你有写在明确收益率,它很容易,以确保它不会在两个关键的操作应该出现原子到其他部分的中间降落在code。

The real answer is that Guido likes the fact that asynchronous yield points are explicit in coroutines, because if you don't realize that a call can yield, then that's an invitation to concurrency problems -- like with threads. But if you have to write an explicit yield from, it's fairly easy to make sure it doesn't land in the middle of two critical operations that should appear atomic to the rest of the code.

至于他提到的在他PYCON 2013主题演讲,有喜欢GEVENT,其他Python框架异步其中默认情况下异步的,他不喜欢这种做法。 (11:58时):

As he mentions in his PyCon 2013 keynote, there are other Python async frameworks like Gevent, which are async by default, and he doesn't like that approach. (at 11:58):

不幸的是你还没有完全清楚的问题
  该调度程序可以在随机时刻打断你的任务,
  切换到不同的一个。 [...]你今天调用任何函数
  你碰巧知道,它从来没有切换,明天有人可能会增加
  日志记录语句或懒惰或缓存的设置咨询
  文件。 [...]

And unfortunately you're still not completely clear of the problem that the scheduler could at a random moment interrupt your task and switch to a different one. [...] Any function that you call today that you happen to know that it never switches, tomorrow someone could add a logging statement or a lazy caching or a consulting of a settings file. [...]

这篇关于郁金香/ ASYNCIO:为什么不能所有的呼叫是异步,并指定时,事情应该是同步的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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