-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple.py
More file actions
104 lines (80 loc) · 2.97 KB
/
simple.py
File metadata and controls
104 lines (80 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import time
import pyloader
def progress_callback(progress):
"""!!! IMPORTANT !!!
This callback will NOT be called from a subsequent thread but will use
the downloads thread instead.
This means the download blocks for as long as the callback is executed.
Usually you only want to persist/visualize the progress.
"""
# Use `help(pyloader.Progress)` to see all the goodies
print(progress.dlable.file_name, '{0:.2f}%'.format(progress.percent))
# `return True` if the download should be canceled
return False
def url_resolver(item):
"""At times you may have urls that would expire while the item is queued.
This callback will be called right before the download starts and allowes
you to alter the `DLable` instance (and the url that goes along with it).
"""
# item.url = 'http://new.url'
return item
if __name__ == '__main__':
# Create a loader instance
loader = pyloader.Loader.get_loader()
loader.configure(
max_concurrent=3,
update_interval=1,
progress_cb=progress_callback,
url_resolve_cb=url_resolver,
daemon=False
)
# Start the loader
# Make sure you know how the `daemon` flag
# affects the liftime of your program
loader.start()
# Create downloadable items
item1 = pyloader.DLable(
url='http://download.thinkbroadband.com/5MB.zip',
target_dir='./',
file_name='item1.zip'
)
item2 = pyloader.DLable(
url='http://download.thinkbroadband.com/5MB.zip',
target_dir='./',
file_name='item2.zip'
)
# Queue an item or...
loader.queue(item1)
# ...alternatively you can force a download,
# ignoring `max_concurrent`
loader.download(item2)
# If you don't use a callback (which would allow you to cancel a
# download if necessary), you can `stop` one too
loader.stop(item1)
# or via its uid
loader.stop(item1.uid)
# You can also clear all queued items
loader.clear_queued()
# True if both necessary main threads are still alive and kicking
print('Is alive:', loader.is_alive())
# True if items are queued and/or downloading
print('Is active:', loader.is_active())
# Amount of queued items
print('Queued items:', loader.queued)
# Amount of active/downloading items
print('Active items:', loader.active)
# The amount of maximum concurrent allowed downloads
print('Max concurrent:', loader.max_concurrent)
# Change the amount of max concurrent downloads
# Will also trigger new downloads if necessary
loader.max_concurrent = 5
# How often the progress callback will be called in seconds
print('Update interval:', loader.update_interval)
# Change the interval
loader.update_interval = 0.5
# Wait for all downloads to finish
while loader.is_active():
time.sleep(0.25)
# Exit downloader and stop all downloads
# `pyloader` does NOT persist anything. This is up to you!
loader.exit()