Send async generator using Python requests module

94 Views Asked by At

I'm using requests module in Python and I want to send data in request streaming way. so I applied chunked-encoded request according to this document.

However, the data to send is provided with another async method. Honestly speaking, I also use Sanic framework, and I read the stream data from await sanic.request.stream.read() and I want to relay the streaming data to request method of requests module.

async def generate_from_sanic_request(request):
  while True:
    chunk = await request.stream.read()
    if chunk is None:
      break
    yield chunk

...
requests.request(
  method="POST",
  url="my-own-url",
  data=generate_from_sanic_request, # here is the point of this question.
)
...

The document says that I can use the non-async generator function as the data argument. However, when I use an async generator there, requests.request cannot handle the generator.

I want to do this in a memory-efficient way, so I want to bypass the received streaming request body to requests.request method in one-by-one way.

Can I get any help to solve this problem?

Thanks in advance.

0

There are 0 best solutions below