from threading import Thread, Timer
from functools import partial
import time
import urllib
def check_connectivity(t):
try:
urllib.request.urlopen("http://google.com", timeout=2)
except Exception as e:
t._Thread__stop()
class Download(Thread):
def run(self):
print("Trying to download file....")
con = partial(check_connectivity, self)
while True:
t = Timer(5, con) # Checks the connectivity every 5 second or less.
t.start()
# your download code....
def main():
down = Download()
down.start()
down.join()
一个简单的解决方案是将下载代码移动到一个线程,并使其成为一个可以中断的单独进程。你知道吗
可以使用python线程和定时器模块来实现。你知道吗
代码将主下载循环移动到线程的run方法中。启动一个计时器,在里面监听网络连接。你知道吗
相关问题 更多 >
编程相关推荐