简体   繁体   English

Python:使用SIGTERM中断urllib2.urlopen()

[英]Python: interrupting urllib2.urlopen() with SIGTERM

I'm using urllib2.urlopen() to open sometimes potentially large files. 我正在使用urllib2.urlopen()打开有时可能很大的文件。 I have a signal handler to catch SIGTERM, but is it possible to actually interrupt urlopen() when it's downloading a big file to close my program immediately, without waiting for the call to finish? 我有一个捕获SIGTERM的信号处理程序,但是当它下载一个大文件以立即关闭程序时,是否有可能实际上中断urlopen(),而无需等待调用完成?

urlopen returns a file-like object. urlopen返回一个类似文件的对象。 Data is only sent over the network when you make a .read() request on this object. 仅当您对此对象发出.read()请求时,才通过网络发送数据。 (Your OS does some buffering of network data, so this isn't strictly true, but it's close enough for all practical purposes.) (您的操作系统对网络数据进行了一些缓冲,因此严格来说并非如此,但是对于所有实际目的而言,它已经足够接近了。)

So simply use the .read() method's capability to read data in chunks using a loop, perhaps 16K or 64K at a time, rather than retrieving the whole file at once. 因此,只需使用.read()方法的功能就可以使用循环(一次可能是16K或64K)一次读取块中的数据,而不是一次检索整个文件。 In your signal handler, then, you can close the file-like object and the file will stop downloading after the current chunk finishes. 然后,在信号处理程序中,您可以关闭类似文件的对象,并且在当前块完成后文件将停止下载。 The smaller the chunk you use, the less latency there will be in stopping the download. 您使用的块越小,停止下载的延迟就越少。

I'd use a global variable to hold the reference to file-like object so it is accessible in your signal handler; 我将使用全局变量来保存对类似文件的对象的引用,以便可以在信号处理程序中对其进行访问; in this case it seems like the simplest solution. 在这种情况下,这似乎是最简单的解决方案。

If you should happen to try to read from the file-like object after closing it, you will get an exception, which you can handle gracefully. 如果您碰巧在关闭文件状对象后尝试读取该文件,则会得到一个异常,可以妥善处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM