- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有没有办法手动退出trio无限循环,就像trio教程中的echo client,https://trio.readthedocs.io/en/latest/tutorial.html#an-echo-client ,除了使用 Ctrl-C
或使用超时?
我的想法是使用从另一个 python 脚本调用 echo 客户端,并能够使用相同的 python 脚本任意关闭它。我正在考虑使用一个标志(也许是事件?)作为触发托儿所中的 cancel_scope.cancel()
的开关。但我不知道如何触发开关。下面是我修改教程 echo 客户端代码的尝试。
import sys
import trio
PORT = 12345
BUFSIZE = 16384
FLAG = 1 # FLAG is a global variable
async def sender(client_stream):
print("sender: started")
while FLAG:
data = b'async can sometimes be confusing but I believe in you!'
print(f"sender: sending {data}")
await client_stream.send_all(data)
await trio.sleep(1)
async def receiver(client_stream):
print("recevier: started!")
while FLAG:
data = await client_stream.receive_some(BUFSIZE)
print(f"receiver: got data {data}")
if not data:
print("receiver: connection closed")
sys.exit()
async def checkflag(nursery): # function to trigger cancel()
global FLAG
if not FLAG:
nursery.cancel_scope.cancel()
else:
# keep this task running if not triggered, but how to trigger it,
# without Ctrl-C or timeout?
await trio.sleep(1)
async def parent():
print(f"parent: connecting to 127.0.0.1:{PORT}")
client_stream = await trio.open_tcp_stream("127.0.0.1", PORT)
async with client_stream:
async with trio.open_nursery() as nursery:
print("parent: spawning sender ...")
nursery.start_soon(sender, client_stream)
print("parent: spawning receiver ...")
nursery.start_soon(receiver, client_stream)
print("parent: spawning checkflag...")
nursery.start_soon(checkflag, nursery)
print('Close nursery...')
print("Close stream...")
trio.run(parent)
我发现我无法在 trio.run()
之后向 python REPL 输入任何命令,以手动更改 FLAG
,我想知道我是否调用这个来自另一个脚本的 echo 客户端,究竟如何触发 nursery 中的 cancel_scope.cancel()
?或者,还有更好的方法?非常感谢所有帮助。谢谢。
最佳答案
如果您想使用键盘输入退出,这里有一个适用于 Linux 和 Mac OS X 的解决方案。您可以使用 Python msvcrt模块在 Windows 上执行类似的操作。
我从 the Trio tutorial 复制了 echo-client.py
并在添加的三个代码块上添加"new"注释。在 REPL 中,您可以键入“q”来取消 nursery 作用域并退出:
# -- NEW
import termios, tty
import sys
import trio
PORT = 12345
BUFSIZE = 16384
# -- NEW
async def keyboard():
"""Return an iterator of characters from stdin."""
stashed_term = termios.tcgetattr(sys.stdin)
try:
tty.setcbreak(sys.stdin, termios.TCSANOW)
while True:
yield await trio.run_sync_in_worker_thread(
sys.stdin.read, 1,
cancellable=True
)
finally:
termios.tcsetattr(sys.stdin, termios.TCSANOW, stashed_term)
async def sender(client_stream):
print("sender: started!")
while True:
data = b"async can sometimes be confusing, but I believe in you!"
print("sender: sending {!r}".format(data))
await client_stream.send_all(data)
await trio.sleep(1)
async def receiver(client_stream):
print("receiver: started!")
while True:
data = await client_stream.receive_some(BUFSIZE)
print("receiver: got data {!r}".format(data))
if not data:
print("receiver: connection closed")
sys.exit()
async def parent():
print("parent: connecting to 127.0.0.1:{}".format(PORT))
client_stream = await trio.open_tcp_stream("127.0.0.1", PORT)
async with client_stream:
async with trio.open_nursery() as nursery:
print("parent: spawning sender...")
nursery.start_soon(sender, client_stream)
print("parent: spawning receiver...")
nursery.start_soon(receiver, client_stream)
# -- NEW
async for key in keyboard():
if key == 'q':
nursery.cancel_scope.cancel()
trio.run(parent)
调用 tty.setcbreak
将终端置于无缓冲模式,因此您不必在程序接收输入之前按回车键。它还可以防止字符回显到屏幕上。此外,顾名思义,它允许 Ctrl-C
正常工作。
在 finally
block 中,termios.tcsetattr
将终端恢复到 tty.setcbreak
之前的任何模式。所以你的终端在退出时恢复正常。
sys.stdin.read
是在一个单独的线程中产生的,因为它需要以阻塞模式运行(在异步上下文中不好)。原因是 stdin
shares its file description使用 stdout
和 stderr
。将 stdin
设置为非阻塞也会将 stdout
设置为非阻塞作为副作用,这可能会导致 print
函数出现问题(在我的情况下被截断)。
进程间通信
这是一个使用套接字从另一个取消一个 Trio 进程的基本示例:
# infinite_loop.py
import trio
async def task():
while True:
print("ping")
await trio.sleep(0.5)
async def quitter(cancel_scope):
async def quit(server_stream):
await server_stream.receive_some(1024)
cancel_scope.cancel()
await trio.serve_tcp(quit, 12346)
async def main():
async with trio.open_nursery() as nursery:
nursery.start_soon(task)
nursery.start_soon(quitter, nursery.cancel_scope)
trio.run(main)
# slayer.py
import trio
async def main():
async with await trio.open_tcp_stream("127.0.0.1", 12346) as s:
await trio.sleep(3)
await s.send_all(b'quit')
trio.run(main)
关于python - 如何手动退出无限三重循环,就像三重奏的教程 echo client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56632002/
我有一个 HTML 文件,当您单击按钮时,Ajax 会加载一个 PHP 文件,然后 PHP 文件将一些文本回显到 HTML 文件。 问题是,如果我第二次(或第三次、第四次等)单击该按钮,下一个 ech
我正在尝试使用以下方法调用数据库中的 HTML/PHP 内容: 当调用该行时,HTML 会正确显示,但 PHP 不会。我相信这是主 echo 内部 echo 的原因。 Hello \
我正在尝试使用复选框来显示和更新 MySQL 数据库中的记录。我想根据复选框是选中还是未选中来更改复选框的值。但是我收到了这个错误: 解析错误:语法错误,意外的 T_ENCAPSED_AND_WHIT
我遇到了重定向符号的不同位置( > , &2 message message echo message >&2 message >&2 echo message message 对于所有表单,我得到了
这个问题在这里已经有了答案: Source files in a bash script (2 个回答) 2年前关闭。 https://askubuntu.com/questions/250012/h
我注意到当我在 DOS 中使用 echo 将某些内容打印到文件时,会在字符串后附加一个空格。我需要打印没有尾随空格的字符串。有没有办法做到这一点,或者作为一种解决方法,从文件中删除尾随空格? 最佳答案
不确定我在这里做错了什么,但结果总是空的。只有当没有选择答案时,脚本才应该输出“你没有选择答案”,否则它应该输出给定的答案: 我已经按照提到的更新了脚本,但即使给出了答案,仍然得到空输出:/ 感谢到目
不确定我在这里做错了什么,但结果总是空的。只有当没有选择答案时,脚本才应该输出“你没有选择答案”,否则它应该输出给定的答案: 我已经按照提到的更新了脚本,但即使给出了答案,仍然得到空输出:/ 感谢到目
echo 'Welcome echo $_SESSION["username"];'; 在上面的代码中,它没有显示用户名变量。相反,它只是简单地回显 Welcome $_SESSION["userna
我试图在 php echo 语句中回显一个 php 变量。 我在 MySql 数据库中有一个名为 display_code 的列,其中包含一个值,例如 $num_rows_customers。 我从数
如上所述,我想知道为什么我们使用@Echo off/on 而不是Echo off/on。这是有什么原因还是只是为了可见性?根据这个,它在视觉上似乎没有什么不同: C:\Users\Jack> Echo
我希望能够将新的 ECHO 消息连接到先前的 ECHO 消息,如下所示: 命令: ECHO PROCESSING... REM some process here ECHO DONE 结果: PROC
我不了解 jQuery,所以如果这是一个愚蠢的问题,我很抱歉。 当我将值从 php 传递到 javascript 时,即使之前用 echo 打印的消息也被传递,我怎样才能避免这种情况? php $
请有人解释一下下面的结果差异 echo intval(1e10); 输出1410065408 echo 1e10; 输出10000000000 最佳答案 有符号整数有最大值。在 32 位系统上,它
我正在制作导航菜单,我想添加一个事件类这是我的代码 我想输出 CSTYLE 并使用 'if 来回显事件类 %s %s ', $row['CSTYLE'], $row['id'], $row['na
echo -n 的终端手册页如下: -n Do not print the trailing newline character. This may also be achie
直到今天它突然停止工作时,它一直工作得很好......(我知道这不是很有帮助,但我到处都看了看) 我正在遍历从 mySQL 查询返回的值,并将每个值放入一个数组中,然后将这个数组放入另一个数组中。然后
目前我正在这样做: $lines = file('data/index'); foreach ($lines as $value) list($title, $location) =
我在 PHP CLI 中运行以下脚本: 什么都没有显示。如何启用输出? 最佳答案 在您的终端中运行它: php -r 'echo "Hello World!\n";' 关于echo - 在 PHP
希望这是一个简单的问题... 我正在使用批处理文件来移动一些文件,不幸的是,出于各种原因,这有点复杂。要正常运行,需要右键单击并“以管理员身份运行”。 为了提醒未清洗的大众和我执行该步骤,我希望第一行
我是一名优秀的程序员,十分优秀!