Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO.output_gen 无法在超时时正确结束程序 #156

Closed
weilycoder opened this issue Dec 2, 2024 · 1 comment · Fixed by #155
Closed

IO.output_gen 无法在超时时正确结束程序 #156

weilycoder opened this issue Dec 2, 2024 · 1 comment · Fixed by #155

Comments

@weilycoder
Copy link
Contributor

weilycoder commented Dec 2, 2024

未在其他系统测试。 已在 Linux 下测试。

import sys
from cyaron import IO

p = IO(None, None)
p.output_gen(f'"{sys.executable}" test.py', time_limit=1.0)
# test.py
import sys

while True:
    print("End", file=sys.stderr)

观察到程序不断输出 End,证明程序并未在超时后中止。

相关链接:https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true

按照链接中内容,超时后中止的只有 Shell,而没有在 Shell 上运行的子进程。

@weilycoder weilycoder changed the title IO.output_gen 无法在超时时正确结束程序(Windows 下) IO.output_gen 无法在超时时正确结束程序 Dec 3, 2024
@weilycoder
Copy link
Contributor Author

已知的解决方法有两种:

  1. 删除参数 shell=True,这可能导致部分生成器失效(例如,若使用管道符,那么需要指定 shell=True)。
  2. 手动在 TimeoutExpired 被抛出时结束进程及其所有子进程,我已知的跨平台的实现方案需要使用第三方库 psutil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant