- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个服务器应用程序,当客户请求时,我会安排一些工作,比如
def work():
time.sleep(5)
fut = asyncio.get_event_loop().run_in_executor(None, work)
我 await fut
稍后当明确请求时。我的用例要求 run_in_executor
立即提交 work
函数,这在我的环境(Ubuntu 16.04、Python 3.7.1)中的表现符合预期。
由于我的应用程序依赖于此行为,所以我想验证它不会发生变化,因此我检查了几个资源:
asyncio.Future
。run_in_executor
是协程。run_in_executor
是返回可等待对象的函数还是协程本身似乎被视为实现细节。参见 25675和 32327 .AbstractEventLoop.run_in_executor
被指定为协程,但在BaseEventLoop.run_in_executor
中实现是一个简单的函数。1 和 2 似乎主要表明当前行为是正确的,但 3 和 4 是令人担忧的。这似乎是接口(interface)的一个非常重要的部分,因为如果函数本身是协程,那么它不会开始执行(因此不会安排工作)直到它被等待。
依赖当前行为是否安全?如果是这样,将 AbstractEventLoop.run_in_executor
的接口(interface)改为普通函数而不是协程是否合理?
最佳答案
My use case requires that
run_in_executor
submit the work function immediately, and that behaves as expected in my environment
documentation 不保证当前行为不 ,它只指定函数 arranges 为 func
调用,并且它返回一个 awaitable。如果它是用协程实现的,它将在事件循环运行之前不会提交。
但是,此行为自 the beginning 以来一直存在并且在未来极不可能改变。延迟提交,尽管文档在技术上允许,但会破坏许多现实世界的异步应用程序,并构成严重的向后不兼容的更改。
如果您想确保任务启动时不依赖于未记录的行为,您可以创建自己的等同于 run_in_executor
的函数。真的boils down结合executor.submit
和 asyncio.wrap_future
.没有多余的装饰,它可以像这样简单:
def my_run_in_executor(executor, f, *args):
return asyncio.wrap_future(executor.submit(f, *args))
因为函数中直接调用了executor.submit
,所以这个版本保证了worker函数的启动,而无需等待事件循环运行。
PEP 3156明确声明 run_in_executor
是“等同于 wrap_future(executor.submit(callback, *args))
”,从而提供所需的保证——但 PEP 不是官方文档,最终的实现和规范通常与最初的 PEP 不同。
如果坚持使用 run_in_executor
的文档化接口(interface),也可以使用显式同步来强制协程等待 worker 启动:
async def run_now(f, *args):
loop = asyncio.get_event_loop()
started = asyncio.Event()
def wrapped_f():
loop.call_soon_threadsafe(started.set)
return f(*args)
fut = loop.run_in_executor(None, wrapped_f)
await started.wait()
return fut
fut = await run_now(work)
# here the worker has started, but not (necessarily) finished
result = await fut
# here the worker has finished and we have its return value
这种方法引入了不必要的实现和接口(interface)复杂性,特别是需要使用 await
来获得 future,这与 asyncio 的正常工作方式背道而驰。 run_now
仅用于完整性目的,我不建议在生产中使用它。
关于python - asyncio.run_in_executor 是否指定不明确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54263558/
我正在我的 java 作业中使用 GUI,并且我必须指定 JCheckBox 中的其他内容。除了这个小要求,其他的我都完成了。我不太确定如何解决这个问题,我查阅了我的书并尝试在线研究 要求: 一系列复
在各种语言中(我将在这里使用 JavaScript,但我已经在 PHP 和 C++ 中以及可能在其他地方看到过它),似乎有几种构造简单 for 循环的方法。版本 1 如下: var top = doc
有没有一种方法可以使用 CSS 指定每次“小于符号”(在键盘上 M 的右侧)或“大于符号”出现在文本中时,它应该被替换为分别是“小于”或“大于”的实际词? 最佳答案 CSS 不能作用于(不能修改,即)
首先,使用 setspn 命令为用户注册服务主体名称。 setspn -a CS/dummy@abc.com dummyuser setspn -l dummyuser 给出输出为 CS/dummy@
我在指定从 SFSafariViewController 访问时遇到问题,因为它具有与 Safari 浏览器完全相同的用户代理。 我要做的是仅在 webview 内显示图片,如果在普通浏览器上查看,则
我正在尝试用 R 语言在 lavaan 中指定一个奇怪的模型。该模型如下所示: 我的规范尝试如下所示。我发现难以实现的是将观察到的变量的唯一误差固定为唯一项的两个相关性的总和。 例如,项目 y*1,2
我正在构建 API 以将我的 React 应用程序与我的后端服务连接起来,我想使用 typescript 来指定 data 的类型在我的 Axios 请求中。如何在不修改其他字段的情况下更新 Axio
如何为模型指定初始“软”值?该初始模型是解决类似查询的结果,并且该模型很可能具有正确的部分,甚至对于当前查询可能是正确的。 目前,我正在通过增量求解和 hard/soft constraints 对此
我有来自网页的以下代码 https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+Producer+Example 似乎缺少的是如何配置分区数。我
有没有办法在每个查询的基础上在 Neo4jClient 中指定 Cypher 解析器的版本,如 here 所述? 谢谢! 最佳答案 如果您将 Neo4jClient 更新到最新版本(> 1.0.0.6
我有以下代码生成四个图,但它们最终被压扁(见下图)。我该如何解决这个问题? par(mfrow=c(2,2)) curve(.5*exp(-.5*x),from=0,to=10,main="f(x)"
我有一个 ColdFusion 10 服务器。我正在使用 JDBC 驱动程序连接到 db2 数据库。我偶然发现了这个笔记。这个设置在哪里?我还查看了 neo*.xml 文件,但没有看到任何 db 驱动
我想知道是否可以指定验证器的运行顺序。 目前,我编写了一个自定义验证器,检查它是否为 [a-zA-Z0-9]+ 以确保登录验证我们的规则,并编写了一个远程验证器以确保登录可用,但目前远程验证器已启动在
我的应用程序需要至少 40MB 的 RAM,因此早期的 iPhone(例如 3G、第一个 iPod touch 版本)就没有它(它们为我的应用程序提供的最大内存约为 20MB)。有没有正确的方法来禁用
我有一个保存日期(不是当前日期)的 Date 对象,我需要以某种方式指定该日期为 UTC,然后将其转换为“欧洲/巴黎”,即 +1 小时。 public static LocalDateTime toL
我想问你在 Varnish 代码中如何在没有缓存的情况下将请求传递到后端。 我知道我可以做到并且正在发挥作用: if (req.url ~ "(\?|&)(something|somethin
我目前基于模块编译程序(如主程序 foo 依赖于模块 bar )如下: gfortran -c bar.f90 gfortran -o foo.exe foo.f90 bar.o 这在 foo.f90
我正在尝试创建一个依赖于另一个 meteor 包的新 meteor 包。当我尝试 meteor add mypackage 时,出现以下错误。为什么 Meteor 不添加 mypackage 并引入它
我正在制作执行器/ react 器,同时发现这是一个终生的问题。它与 async/Future 无关,可以在没有 async 糖的情况下进行复制。 use std::future::Future; s
我在 cassandra 中有一个表,其数据类型为时间戳。我正在使用 cqlsh 从数据库中获取数据,并希望更改我的时间戳列输出的输出格式。我研究了一下,发现我可以通过更改以下文件来更改时间戳输出格式
我是一名优秀的程序员,十分优秀!