Skip to content

Instantly share code, notes, and snippets.

@IuryAlves
Created August 12, 2018 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IuryAlves/76c53408ec33fb20521fe675db46c4b3 to your computer and use it in GitHub Desktop.
Save IuryAlves/76c53408ec33fb20521fe675db46c4b3 to your computer and use it in GitHub Desktop.
# 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