Created
August 12, 2018 03:13
-
-
Save IuryAlves/76c53408ec33fb20521fe675db46c4b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import requests | |
from multiprocessing import Process, Pipe | |
def downloader(url, chunk_size, conn): | |
downloaded_parts = 0 | |
with open('download', 'wb') as fp: | |
response = requests.get(url, stream=True) | |
for content in response.iter_content(chunk_size): | |
downloaded_parts += 1 | |
fp.write(content) | |
conn.send(downloaded_parts) | |
conn.send(-1) # send to indicate that download is complete | |
conn.close() | |
def main(): | |
chunk_size = 1024 * 1024 | |
download_url = 'http://speedtest.ftp.otenet.gr/files/test10Mb.db' | |
parent_conn, child_conn = Pipe() | |
downloader_process = Process(target=downloader, args=(download_url, chunk_size, child_conn)) | |
downloader_process.start() | |
# read data from downloader_process | |
result = parent_conn.recv() | |
while result != -1: | |
print('#' * result) | |
result = parent_conn.recv() | |
downloader_process.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment