- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试向我现有的 asyncio 循环添加一些代码,以在 Ctrl-C 上提供干净的关闭。下面是它正在做的事情的抽象。
import asyncio, signal
async def task1():
print("Starting simulated task1")
await asyncio.sleep(5)
print("Finished simulated task1")
async def task2():
print("Starting simulated task2")
await asyncio.sleep(5)
print("Finished simulated task2")
async def tasks():
await task1()
await task2()
async def task_loop():
try:
while True:
await asyncio.shield(tasks())
await asyncio.sleep(60)
except asyncio.CancelledError:
print("Shutting down task loop")
raise
async def aiomain():
loop = asyncio.get_running_loop()
task = asyncio.Task(task_loop())
loop.add_signal_handler(signal.SIGINT, task.cancel)
await task
def main():
try:
asyncio.run(aiomain())
except asyncio.CancelledError:
pass
#def main():
# try:
# loop = asyncio.get_event_loop()
# loop.create_task(aiomain())
# loop.run_forever()
# except asyncio.CancelledError:
# pass
if __name__ == '__main__':
main()
task1
的序列和
task2
需要在启动后完成,否则一些工件将处于不一致的状态。 (因此
asyncio.shield
包装器围绕着调用
tasks
。)
Starting simulated task1
然后循环停止和
task2
永远不会开始。如果我尝试切换到
main
的版本这被注释掉了,即使循环被正确取消并且至少在几分钟内没有进一步发生,它也永远不会退出。它确实有一些进步,因为它至少完成了任何正在进行的
task1
序列。和
task2
.
asyncio.shield
周围创建一个包装器它增加了一个由 asyncio.Condition
同步的变量对象,运行屏蔽函数,然后递减变量。然后,在 aiomain
在 CancelledError
处理程序,在重新引发异常之前等待变量达到零。 (在一个实现中,我可能会使用 __aexit__
将它的所有部分组合到一个类中,在 CancelledError
逻辑上实现等待零。) asyncio
的取消机制,而是使用 asyncio.Event
或类似的允许中断点或可中断的 sleep 。尽管这看起来似乎更具侵入性,需要我指定哪些点被认为是可中断的,而不是声明哪些序列需要避免取消。 最佳答案
这个问题问得好。我在制定答案时学到了一些东西,所以我希望你仍然关注这个线程。
首先要调查的是,shield() 方法是如何工作的?在这一点上,至少可以说文档令人困惑。直到我阅读了 test_tasks.py 中的标准库测试代码,我才弄明白。以下是我的理解:
考虑这个代码片段:
async def coro_a():
await asyncio.sheild(task_b())
...
task_a = asyncio.create_task(coro_a())
task_a.cancel()
import asyncio
import signal
import time
async def task1():
print("Starting simulated task1", time.time())
await asyncio.sleep(5)
print("Finished simulated task1", time.time())
async def task2():
print("Starting simulated task2", time.time())
await asyncio.sleep(5)
print("Finished simulated task2", time.time())
async def tasks():
await task1()
await task2()
async def task_loop():
try:
while True:
await asyncio.shield(tasks())
await asyncio.sleep(60)
except asyncio.CancelledError:
print("Shutting down task loop", time.time())
raise
async def aiomain():
task = asyncio.create_task(task_loop())
KillNicely(task)
try:
await task
except asyncio.CancelledError:
print("Caught CancelledError", time.time())
await asyncio.sleep(5.0)
raise
class KillNicely:
def __init__(self, cancel_me):
self.cancel_me = cancel_me
self.old_sigint = signal.signal(signal.SIGINT,
self.trap_control_c)
def trap_control_c(self, signum, stack):
if signum != signal.SIGINT:
self.old_sigint(signum, stack)
else:
print("Got Control-C", time.time())
print(self.cancel_me.cancel())
def main():
try:
asyncio.run(aiomain())
except asyncio.CancelledError:
print("Program exit, cancelled", time.time())
# Output when ctrlC is struck during task1
#
# Starting simulated task1 1590871747.8977509
# Got Control-C 1590871750.8385916
# True
# Shutting down task loop 1590871750.8425908
# Caught CancelledError 1590871750.8435903
# Finished simulated task1 1590871752.908434
# Starting simulated task2 1590871752.908434
# Program exit, cancelled 1590871755.8488846
if __name__ == '__main__':
main()
import asyncio
import signal
import time
async def task1():
print("Starting simulated task1", time.time())
await asyncio.sleep(5)
print("Finished simulated task1", time.time())
async def task2():
print("Starting simulated task2", time.time())
await asyncio.sleep(5)
print("Finished simulated task2", time.time())
async def tasks(kwrap):
fut = asyncio.get_running_loop().create_future()
kwrap.awaitable = fut
await task1()
await task2()
fut.set_result(1)
async def task_loop(kwrap):
try:
while True:
await asyncio.shield(tasks(kwrap))
await asyncio.sleep(60)
except asyncio.CancelledError:
print("Shutting down task loop", time.time())
raise
async def aiomain():
kwrap = KillWrapper()
task = asyncio.create_task(task_loop(kwrap))
KillNicely(task)
try:
await task
except asyncio.CancelledError:
print("Caught CancelledError", time.time())
await kwrap.awaitable
raise
class KillNicely:
def __init__(self, cancel_me):
self.cancel_me = cancel_me
self.old_sigint = signal.signal(signal.SIGINT,
self.trap_control_c)
def trap_control_c(self, signum, stack):
if signum != signal.SIGINT:
self.old_sigint(signum, stack)
else:
print("Got Control-C", time.time())
print(self.cancel_me.cancel())
class KillWrapper:
def __init__(self):
self.awaitable = asyncio.get_running_loop().create_future()
self.awaitable.set_result(0)
def main():
try:
asyncio.run(aiomain())
except asyncio.CancelledError:
print("Program exit, cancelled", time.time())
# Run 1 Control-C during task1
# Starting simulated task1 1590872408.6737766
# Got Control-C 1590872410.7344952
# True
# Shutting down task loop 1590872410.7354996
# Caught CancelledError 1590872410.7354996
# Finished simulated task1 1590872413.6747622
# Starting simulated task2 1590872413.6747622
# Finished simulated task2 1590872418.6750958
# Program exit, cancelled 1590872418.6750958
#
# Run 1 Control-C during task2
# Starting simulated task1 1590872492.927735
# Finished simulated task1 1590872497.9280624
# Starting simulated task2 1590872497.9280624
# Got Control-C 1590872499.5973852
# True
# Shutting down task loop 1590872499.5983844
# Caught CancelledError 1590872499.5983844
# Finished simulated task2 1590872502.9274273
# Program exit, cancelled 1590872502.9287038
#
# Run 1 Control-C after task2 -> immediate exit
# Starting simulated task1 1590873694.2925708
# Finished simulated task1 1590873699.2928336
# Starting simulated task2 1590873699.2928336
# Finished simulated task2 1590873704.2938952
# Got Control-C 1590873706.0790765
# True
# Shutting down task loop 1590873706.0804725
# Caught CancelledError 1590873706.0804725
# Program exit, cancelled 1590873706.0814824
关于python - 只要有待处理的取消屏蔽任务,但不再存在,我如何运行 asyncio 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62076607/
我需要创建一个 View (作为其他 View 的 mask ) 。圆圈是透明的,外部区域是半透明的。我可以使用 canvas.clipRegion(..) 来实现它,但是 http://develo
踏入爬虫的迷宫 多年以来,我一直是一个对编程充满了好奇心的人。探索着代码的世界,便如同探险家踏入密林深处,寻找未知的宝藏。最近,我将目光聚焦在了爬虫技术上,特别是百度搜索屏蔽问题上。 百度搜索的诱惑
我想将“www.adomain.com”重定向到“www.adomain.com/cms”。 cms 部分应该被屏蔽。 我无法让它安静地工作。因此“cms”始终是网址的一部分。 我尝试了这个解决方案:
我一直在我的 View Controller 中使用以下代码: UIView *view = [[CustomView alloc] init]; UIView *mask = [[CustomMas
如果是网页内容里面的alert,我们可以等网页加载完毕,也就是在webViewDidFinishLoad中执行下面的js代码,就可以屏蔽alert了
我正在使用 PyQt4 QLineEdit小部件接受密码。有一个setMasking属性,但不遵循如何设置屏蔽字符。 最佳答案 editor = QLineEdit() editor.setEchoM
我有一个包含员工信息的 Pandas 数据框,如下所示: df=pd.DataFrame({ 'Id':[1,2,3,4], 'Name':['Joe','Henry','Sam','
我正在为 iPhone 创建一个自定义开/关切换开关(类似于标准开关),并且我正在设置 slider 的蒙版,但调用 [[myView [layer] setMask:maskLayer] 设置
我如何能够在 Objective C 中屏蔽 nsimageview?例如,有一个带圆角的 nsimageview。 最佳答案 你不知道。如果你想以 NSImageView 不支持的方式绘制图像,则需
我正在开发一个可以更改边框或矩形 UIImage 的应用程序。边框会有所不同,但看起来 UIImage 是用剪刀剪掉的,或者有什么影响。 做到这一点的最佳方法是什么? 我的第一个想法是准备一堆具有我正
我需要屏蔽数据,就像在 Azure Pipelines 中一样,但位于 Azure 存储库文件内。有没有一种方法可以设置与脚本分开存储的变量,例如在 Azure 管道中: variable = $(S
我没有找到解决问题的简单方法。我想使用 TextInputDialog,您必须在其中键入用户密码,以重置数据库中的所有数据。 TextInputDialog 的问题是它没有屏蔽文本,我不知道有什么选择
我正在寻找一种自行开发的方法来扰乱生产数据以用于开发和测试。我已经构建了几个脚本来生成随机社会安全号码、轮类出生日期、打乱电子邮件等。但我在尝试打乱客户姓名时遇到了困难。我想保留真实姓名,这样我们仍然
我正在尝试使用过滤器来屏蔽 SSN 的前 5 位数字,它应该看起来像这样 XXX-XX-1234 到目前为止我想出了什么: // {{SocialSecurityNumber | ssn}} angu
我没有找到解决问题的简单方法。我想使用 TextInputDialog,您必须在其中键入用户密码,以重置数据库中的所有数据。 TextInputDialog 的问题是它没有屏蔽文本,我不知道有什么选择
我想屏蔽一个 Fortran 数组。这是我目前正在做的方式...... where (my_array <=15.0) mask_array = 1 elsewhere mask_ar
当用户在用户界面上输入时,我需要屏蔽数字,用户应该看到一个屏蔽的数字,但在 Java 代码上,我应该得到整个数字,包括屏蔽的字符用户应该看到什么 4545********9632但在 Java 代码(
我正在使用 javafx 文本字段。我需要输入数字以及应符合格式 ###.###.###.### 的小数其中小数点根本不可编辑,数字可以是任何数字;不允许其他输入。并非所有数字占位符都需要填写,例如它
我必须交付一个小型 C++ 软件。我想用一些实现(我使用了表达式模板)来掩盖头文件,以使最终用户难以辨认,因此他们无法修改它们。我无法将代码包含在静态或动态库中。有什么方法可以屏蔽头文件的内容吗? 谢
我正在开发一个与 gestureRecognizer 配合使用的应用程序。 使用手势可以选择 UIImage(例如 rectangle.png),并且可以使用 UIPopoverView 通过为所选图
我是一名优秀的程序员,十分优秀!