mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-04-20 11:01:17 +02:00
ctrl-c for kill threads
This commit is contained in:
parent
2e17920a3b
commit
2d4dbafd91
@ -16,6 +16,7 @@ socket.setdefaulttimeout(timeout)
|
|||||||
|
|
||||||
class Downloader(object):
|
class Downloader(object):
|
||||||
_instance = None
|
_instance = None
|
||||||
|
kill_received = False
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
if not cls._instance:
|
if not cls._instance:
|
||||||
@ -34,13 +35,13 @@ class Downloader(object):
|
|||||||
try:
|
try:
|
||||||
os.mkdir(folder)
|
os.mkdir(folder)
|
||||||
except os.error, e:
|
except os.error, e:
|
||||||
logger.error('Error %s' % str(e))
|
logger.error('%s error %s' % (threading.currentThread().getName(), str(e)))
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
filename = filename if filename else os.path.basename(urlparse(url).path)
|
filename = filename if filename else os.path.basename(urlparse(url).path)
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(folder, filename), "wb") as f:
|
with open(os.path.join(folder, filename), "wb") as f:
|
||||||
response = requests.get(url, stream=True, timeout=10)
|
response = requests.get(url, stream=True, timeout=timeout)
|
||||||
length = response.headers.get('content-length')
|
length = response.headers.get('content-length')
|
||||||
if length is None:
|
if length is None:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
@ -48,16 +49,14 @@ class Downloader(object):
|
|||||||
for chunk in response.iter_content(2048):
|
for chunk in response.iter_content(2048):
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
except (os.error, IOError), e:
|
except (os.error, IOError), e:
|
||||||
logger.error('Error %s' % e)
|
logger.error('%s error %s' % (threading.currentThread().getName(), str(e)))
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
logger.info('%s %s downloaded.' % (threading.currentThread().getName(), url))
|
logger.info('%s %s downloaded.' % (threading.currentThread().getName(), url))
|
||||||
|
|
||||||
def _download_thread(self, queue, folder=''):
|
def _download_thread(self, queue, folder=''):
|
||||||
while True:
|
while not self.kill_received:
|
||||||
if queue.empty():
|
if queue.empty():
|
||||||
queue.task_done()
|
queue.task_done()
|
||||||
break
|
break
|
||||||
@ -73,7 +72,7 @@ class Downloader(object):
|
|||||||
folder = str(folder)
|
folder = str(folder)
|
||||||
|
|
||||||
if self.path:
|
if self.path:
|
||||||
folder = '%s/%s' % (self.path, folder)
|
folder = os.path.join(self.path, folder)
|
||||||
|
|
||||||
if os.path.exists(path=folder):
|
if os.path.exists(path=folder):
|
||||||
logger.warn('Path \'%s\' already exist' % folder)
|
logger.warn('Path \'%s\' already exist' % folder)
|
||||||
@ -88,8 +87,12 @@ class Downloader(object):
|
|||||||
for thread in self.threads:
|
for thread in self.threads:
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
for thread in self.threads:
|
while len(self.threads) > 0:
|
||||||
thread.join()
|
try:
|
||||||
|
self.threads = [t.join(1) for t in self.threads if t is not None and t.isAlive()]
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
logger.warning('Ctrl-C received, sending kill signal.')
|
||||||
|
self.kill_received = True
|
||||||
|
|
||||||
# clean threads list
|
# clean threads list
|
||||||
self.threads = []
|
self.threads = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user