- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,
with open("foo") as f:
f.read()
(但它可能是文件写入、DNS 查找、任何数量的其他 I/O 操作。)
KeyboardInterrupt
被抛出,终结器运行。
最佳答案
键盘中断事件总是在主线程上捕获,它们不会直接影响其他线程(在某种意义上它们不会因为 Ctrl+C
而被中断)。 src1 src2 (in a comment)
在这里,您有一个长 IO 绑定(bind)操作的示例示例,这让我们有时间在它完成之前将其杀死。 KeyboardInterrupt 可以正常工作。
import random
import threading
def long_io(file_name):
with open(file_name, "w") as f:
i = 0
while i < 999999999999999999999999999:
f.write(str(random.randint(0, 99999999999999999999999999)))
i += 1
t = threading.Thread(target=long_io, args=("foo",), daemon=True)
t.start()
# keep the main thread alive, listening to possible KeyboardInterupts
while t.is_alive():
t.join(1) # try to join for 1 second, this gives a small window between joins in which the KeyboardInterrupt can rise
请注意:
daemon
;这样,在 KeyboardInterrupt 上,主线程不会等到 IO 完成,而是将其杀死。您可以按照 here 的说明使用非守护线程(推荐) ,但是对于这个例子来说,直接杀死它们就足够了。 A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the daemon property or the daemon constructor argument. Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an Event. src
make the child thread daemonic, which means that its parent (the main thread here) will kill it when it exits (only non-daemon threads are not killed but joined when their parent exits) src
t.join()
为此,但我们没有。为什么?因为KeyboardInterrupt
也会受到影响,并且只有在连接完成后才会引发。 the execution in the main thread remains blocked at the line thread.join(). src
关于python - 线程时如何中断 Python I/O 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66696797/
我是一名优秀的程序员,十分优秀!