Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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
You can’t perform that action at this time.