Skip to content

Commit

Permalink
update download_file to make use of collect flow control
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Apr 12, 2019
1 parent 167bc80 commit 6a4d8d9
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions docs/examples/download_file.py
@@ -1,13 +1,31 @@
import io
import functools

from twisted.internet.task import react
from twisted.internet import threads, defer

import treq


def download_file(reactor, url, destination_filename):
destination = open(destination_filename, 'wb')
d = treq.get(url, unbuffered=True)
d.addCallback(treq.collect, destination.write)
d.addBoth(lambda _: destination.close())
return d
def deferToThread(reactor, f, *args, **kwargs):
if reactor is None:
from twisted.internet import reactor
return threads.deferToThreadPool(
reactor, reactor.getThreadPool(),
f, *args, **kwargs
)


@defer.inlineCallbacks
def download_file(reactor, url, dest):
response = yield treq.get(url, reactor=reactor, unbuffered=True)
f = yield deferToThread(reactor, io.open, dest, 'wb')
try:
yield treq.collect(
response,
functools.partial(deferToThread, reactor, f.write),
)
finally:
yield deferToThread(reactor, f.close)

react(download_file, ['http://httpbin.org/get', 'download.txt'])

0 comments on commit 6a4d8d9

Please sign in to comment.