- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在努力学习一些关于 asyncio
的知识,但我遇到了一些意想不到的行为。我设置了一个简单的斐波那契服务器,它支持使用流的多个连接。 fib 计算是递归编写的,因此我可以通过输入大量数字来模拟长时间运行的计算。正如预期的那样,长时间运行的计算会阻塞 I/O,直到长时间运行的计算完成。
不过问题来了。我将斐波那契函数重写为协程。我预计通过从每次递归中产生,控制将回退到事件循环,并且等待 I/O 任务将有机会执行,并且您甚至可以同时运行多个 fib 计算。然而,情况似乎并非如此。
代码如下:
import asyncio
@asyncio.coroutine
def fib(n):
if n < 1:
return 1
a = yield from fib(n-1)
b = yield from fib(n-2)
return a + b
@asyncio.coroutine
def fib_handler(reader, writer):
print('Connection from : {}'.format(writer.transport.get_extra_info('peername')))
while True:
req = yield from reader.readline()
if not req:
break
print(req)
n = int(req)
result = yield from fib(n)
writer.write('{}\n'.format(result).encode('ascii'))
yield from writer.drain()
writer.close()
print("Closed")
def server(address):
loop = asyncio.get_event_loop()
fib_server = asyncio.start_server(fib_handler, *address, loop=loop)
fib_server = loop.run_until_complete(fib_server)
try:
loop.run_forever()
except KeyboardInterrupt:
print('closing...')
fib_server.close()
loop.run_until_complete(fib_server.wait_closed())
loop.close()
server(('', 25000))
如果您 netcat 到端口 25000 并开始输入数字,则该服务器运行良好。但是,如果您开始长时间运行的计算(例如 35),则在第一个计算完成之前不会运行其他计算。事实上,甚至不会处理额外的连接。
我知道事件循环正在反馈递归 fib
调用的 yield ,因此控制必须一路下降。但我认为循环会在“蹦床”返回 fib 函数之前处理 I/O 队列中的其他调用(例如生成第二个 fib_handler
)。
我确定我一定是误会了什么,或者我忽略了某种错误,但我一辈子都找不到它。
我们将不胜感激您提供的任何见解。
最佳答案
第一个问题是您正在调用 yield from fib(n)
fib_handler
内部.包括yield from
意味着 fib_handler
将阻塞直到调用 fib(n)
已完成,这意味着它无法处理您在 fib
期间提供的任何输入在跑。即使您所做的只是 fib
内部的 I/O,您也会遇到这个问题。 .要解决此问题,您应该使用 asyncio.async(fib(n))
(或者最好是 asyncio.ensure_future(fib(n))
,如果你有足够新的 Python 版本)安排 fib
使用事件循环,实际上没有阻塞 fib_handler
.从那里,您可以使用 Future.add_done_callback
准备就绪后将结果写入客户端:
import asyncio
from functools import partial
from concurrent.futures import ProcessPoolExecutor
@asyncio.coroutine
def fib(n):
if n < 1:
return 1
a = yield from fib(n-1)
b = yield from fib(n-2)
return a + b
def do_it(writer, result):
writer.write('{}\n'.format(result.result()).encode('ascii'))
asyncio.async(writer.drain())
@asyncio.coroutine
def fib_handler(reader, writer):
print('Connection from : {}'.format(writer.transport.get_extra_info('peername')))
executor = ProcessPoolExecutor(4)
loop = asyncio.get_event_loop()
while True:
req = yield from reader.readline()
if not req:
break
print(req)
n = int(req)
result = asyncio.async(fib(n))
# Write the result to the client when fib(n) is done.
result.add_done_callback(partial(do_it, writer))
writer.close()
print("Closed")
也就是说,仅此更改仍不能完全解决问题;虽然它将允许多个客户端同时连接和发出命令,但单个客户端仍将获得同步行为。发生这种情况是因为当您调用 yield from coro()
直接在协程函数上,控制权直到 coro()
才交还给事件循环(或由 coro
调用的另一个协程)实际上执行一些非阻塞 I/O。否则,Python 只会执行 coro
没有屈服的控制。这是一个有用的性能优化,因为当您的协程实际上不会执行阻塞 I/O 时将控制权交给事件循环是浪费时间,尤其是考虑到 Python 的高函数调用开销。
在你的例子中,fib
从不做任何 I/O,所以一旦你调用 yield from fib(n-1)
fib
内部本身,事件循环在完成递归之前永远不会再次运行,这将阻止 fib_handler
从读取客户端的任何后续输入直到调用 fib
已经完成了。将您的所有 调用包装到fib
在 asyncio.async
保证每次你创建 yield from asyncio.async(fib(...))
时都会将控制权交给事件循环称呼。当我进行此更改时,除了使用 asyncio.async(fib(n))
在 fib_handler
,我能够同时处理来自单个客户端的多个输入。这是完整的示例代码:
import asyncio
from functools import partial
from concurrent.futures import ProcessPoolExecutor
@asyncio.coroutine
def fib(n):
if n < 1:
return 1
a = yield from fib(n-1)
b = yield from fib(n-2)
return a + b
def do_it(writer, result):
writer.write('{}\n'.format(result.result()).encode('ascii'))
asyncio.async(writer.drain())
@asyncio.coroutine
def fib_handler(reader, writer):
print('Connection from : {}'.format(writer.transport.get_extra_info('peername')))
executor = ProcessPoolExecutor(4)
loop = asyncio.get_event_loop()
while True:
req = yield from reader.readline()
if not req:
break
print(req)
n = int(req)
result = asyncio.async(fib(n))
result.add_done_callback(partial(do_it, writer))
writer.close()
print("Closed")
客户端的输入/输出:
dan@dandesk:~$ netcat localhost 25000
35 # This was input
4 # This was input
8 # output
24157817 # output
现在,即使这可行,我也不会使用此实现,因为它在单线程程序中执行大量 CPU 绑定(bind)工作,该程序还希望在同一线程中提供 I/O 服务。这不会很好地扩展,也不会有理想的性能。相反,我建议使用 loop.run_in_executor
运行对 fib
的调用在后台进程中,它允许 asyncio 线程满负荷运行,还允许我们将调用扩展到 fib
跨多个核心:
import asyncio
from functools import partial
from concurrent.futures import ProcessPoolExecutor
def fib(n):
if n < 1:
return 1
a = fib(n-1)
b = fib(n-2)
return a + b
def do_it(writer, result):
writer.write('{}\n'.format(result.result()).encode('ascii'))
asyncio.async(writer.drain())
@asyncio.coroutine
def fib_handler(reader, writer):
print('Connection from : {}'.format(writer.transport.get_extra_info('peername')))
executor = ProcessPoolExecutor(8) # 8 Processes in the pool
loop = asyncio.get_event_loop()
while True:
req = yield from reader.readline()
if not req:
break
print(req)
n = int(req)
result = loop.run_in_executor(executor, fib, n)
result.add_done_callback(partial(do_it, writer))
writer.close()
print("Closed")
关于python - 异步任务意外延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31888368/
我正在使用一个简单的脚本来延迟加载页面上的所有图像;图像源的路径包含在 data-src 属性中,然后放入 img 标记的实际 src 属性中。几乎大多数(?)延迟加载方法的实现都是如何工作的。 这是
我有一个具有多层 (SKNodes) 背景、游戏层、前景和 HUD 的场景,每个场景中都有多个 SKSpriteNode,用于滚动和您可以收集和点击的对象。 hud 层只有一个 SKSpriteNod
我有一个 Controller 函数来创建一些东西。调用该函数时,将运行 setInterval 来获取项目的状态。 这是服务: (function () { 'use strict';
在我的应用程序中,我播放音频直播,延迟非常重要。我正在使用 AVPlayer,但启动需要 5-6 秒,并且我需要最多 3 秒的延迟。我怎样才能更快地开始播放并减少延迟?设置一个小缓冲区就可以了?如何使
我有一个恼人的问题。我有这个简单的服务器代码(比方说): #!/usr/bin/env python3 import wsgiref.simple_server def my_func(env, st
我是 jquery deferreds 的新手。这里我有一个简单的example 。 谁能告诉我为什么在其他函数完成之前就触发完成函数(“现在是我的时间”)? 这里的人 example还创建一个延迟对
正在放置关闭 之前的标签标记相同的 sa 将它们放在 中部分并指定 defer="defer"属性? 最佳答案 是/否。 是的,因为放置 defer 标签会等到文档加载完毕后再执行。 否,因为放置
我知道Javascript没有delay(500)方法,它会延迟执行500毫秒,所以我一直试图通过使用setTimeout和setInterval来解决这个问题。 for(var i =0; i< 1
我们有一个读写主服务器和复制的从读服务器。在某些网络用例中,数据被发布并立即读取以发送回服务器。立即读取是在读取从属设备上完成的,由于延迟,数据尚未在那里更新。 我知道这可能是复制设置的一个常见问题,
我有以下 dag 设置以从 2015 年开始运行追赶。对于每个执行日期,任务实例在一分钟内完成。但是,第二天的任务仅在 5 分钟窗口内开始。例如。上午 10:00、上午 10:05、上午 10:10
当我在 WatchKit 中推送一个新 Controller 并在新 Controller 的awakeWithContext: 方法中使用 setTitle 时,它需要一秒钟左右来设置标题,直到
我将图像显示为 SVG 文件和文本。 出于某种原因,svg 图像的渲染速度比屏幕的其余部分慢,从而导致延迟,这对用户体验不利。 这种延迟正常吗?我该怎么做才能让整个屏幕同时呈现? Row( ma
我正在考虑在我的应用程序中使用 firebase 动态链接。我需要将唯一标识符从电子邮件生成的链接传递到用户应用程序中。当用户安装了应用程序时,这可以正常工作,但是,我对未安装应用程序的方式有些困惑。
您知道如何使用 JQuery 的延迟方法和一个函数来检测所有已更改的表单并将每个表单作为 Ajax 帖子提交吗? 如果我只列出大量表单提交,我可以得到同样的结果,但如果我使用... $('form.c
我需要一种方法来通过回调获取不同的脚本。这个方法工作正常: fetchScripts:function() { var _this=this; $.when( $.aj
我编写了一个 jquery 脚本,允许我淡入和淡出 div,然后重复。该代码运行良好。但是,当我尝试添加延迟(我希望 div 在淡出之前保持几秒钟)时,它无法正常工作。我尝试在代码中的几个地方添加延迟
我正在努力在延迟、带宽和吞吐量之间划清界限。 有人可以用简单的术语和简单的例子来解释我吗? 最佳答案 水比喻: 延迟 是穿过管子所需的时间。 带宽是管有多宽。 水流量为吞吐量 车辆类比: 从源到目的地
我有一个 CRM 系统,当添加联系人时,我想将他们添加到会计系统中。 我在 CRM 系统中设置了一个 Webhook,将联系人传递给 Azure 函数。 Azure 函数连接到会计系统 API 并在那
我有一个 Android AudioTrack,例如: private AudioTrack mAudioTrack; int min = AudioTrack.getMinBufferSize(sa
我正在 React 中开发一个 TODO 应用程序,并尝试构建将删除选中项目延迟 X 秒的功能,并且如果在这段时间内未选中该框,它将不会被删除。 我遇到的主要问题是当用户在同一 X 秒内检查、取消检查
我是一名优秀的程序员,十分优秀!