Skip to content

Commit 4512019

Browse files
committed
multi treading completed
1 parent 56e86b4 commit 4512019

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

main.py

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
1-
from Image import Download
2-
from functions import *
3-
from UrlFinder import UrlFinder
1+
from Image import Save
2+
from Models.Complete.Image import Image
3+
import os.path;
4+
from Crawler.Page import Page
5+
from Crawler.Image import Image
6+
from Image.Download import Download
7+
import functions
8+
import threading
9+
from queue import Queue
410

511
if __name__ == '__main__':
6-
PAGE_URL = 'http://www.indianexpress.com'
7-
link = UrlFinder(page_url=PAGE_URL, tag='a', attr='href')
8-
print(link.page_url)
9-
#link.feed(link.html_string())
10-
#print(link.get_values())
12+
NUMBER_OF_THREADS = 4
13+
queue = Queue()
14+
page = Page('http://fdfashionbd.com')
15+
links = page.fetch_links()
16+
17+
18+
def create_jobs():
19+
for link in sorted(links):
20+
queue.put(link)
21+
queue.join()
22+
23+
24+
# Create worker threads (will die when main exits)
25+
def create_workers():
26+
for _ in range(NUMBER_OF_THREADS):
27+
t = threading.Thread(target=downloading)
28+
t.daemon = True
29+
t.start()
30+
31+
32+
def downloading():
33+
while True:
34+
try:
35+
link = queue.get()
36+
print(threading.current_thread().name + ' fetching ' + link)
37+
img = Image(link)
38+
39+
images = img.fetch_links()
40+
down = Download(links=images, path=functions.get_folder_name(link))
41+
down.start()
42+
queue.task_done()
43+
print(threading.current_thread().name + ' Done ' + link)
44+
if queue.empty():
45+
break
46+
except:
47+
queue.task_done()
48+
continue
49+
50+
51+
52+
53+
create_workers()
54+
create_jobs()

test.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
from Image import Save
2-
from Models.Complete.Image import Image
3-
import os.path;
4-
from Crawler.Page import Page
5-
from Crawler.Image import Image
6-
from Image.Download import Download
7-
import functions
1+
from queue import Queue
82

9-
if __name__ == '__main__':
10-
page = Page('https://gopostie.com')
11-
links = page.fetch_links()
12-
for link in sorted(links):
13-
print('Downloading ...' + link)
14-
img = Image(link)
15-
images = img.fetch_links()
16-
down = Download(links=images,path=functions.get_folder_name(link))
17-
down.start()
18-
# down = Download()
19-
# down.start()
3+
def do_stuff(q):
4+
while not q.empty():
5+
print(q.get())
6+
q.task_done()
7+
8+
q = Queue(maxsize=0)
9+
10+
for x in range(20):
11+
q.put(x)
12+
13+
do_stuff(q)

0 commit comments

Comments
 (0)