toolbelt
toolbelt copied to clipboard
Allow to abort Upload with MultipartEncoderMonitor
For uploading large files with MultipartEncoderMonitor
, it would be helpful to be able to abort the upload. I think this could be done conveniently with the callback
function, i.e. instead of https://github.com/requests/toolbelt/blob/70c1abaab7c14072e8214c3eb8e9c585f4d98a95/requests_toolbelt/multipart/encoder.py#L400-L404
one could do something like:
def read(self, size=-1):
string = self.encoder.read(size)
self.bytes_read += len(string)
cb = self.callback(self)
if cb is None:
# The callback function normally returns `None`.
return string
else:
# If it returns something else, the upload is stopped by raising an Exception.
# This exception can be caught in a try-except statement wrapping around
# the `requests.post(...` call.
raise MultipartEncoderAbort("MultipartEncoder streaming aborted by callback: {}".format(cb))
What do you think? Could something like this be included in requests_toolbelt or should I subclass MultipartEncoderMonitor
in my own projects for this purpose?
The hard thing to reason about here is how this interacts with Requests and the remote server. As suggested, this doesn't seem wise.