@@ -306,41 +306,42 @@ async def setup_code(
306306
307307async def run_python_code_http (application : ASGIApplication , scope : dict ) -> tuple [dict , dict , str , Optional [bytes ]]:
308308 logger .debug ("Running code" )
309- with StringIO () as buf , redirect_stdout (buf ):
310309 # Execute in the same process, saves ~20ms than a subprocess
311310
312- # The body should not be part of the ASGI scope itself
313- scope_body : bytes = scope .pop ("body" )
311+ # The body should not be part of the ASGI scope itself
312+ scope_body : bytes = scope .pop ("body" )
314313
315- async def receive ():
316- type_ = "http.request" if scope ["type" ] in ("http" , "websocket" ) else "aleph.message"
317- return {"type" : type_ , "body" : scope_body , "more_body" : False }
314+ async def receive ():
315+ type_ = "http.request" if scope ["type" ] in ("http" , "websocket" ) else "aleph.message"
316+ return {"type" : type_ , "body" : scope_body , "more_body" : False }
318317
319- send_queue : asyncio .Queue = asyncio .Queue ()
318+ send_queue : asyncio .Queue = asyncio .Queue ()
320319
321- async def send (dico ):
322- await send_queue .put (dico )
320+ async def send (dico ):
321+ await send_queue .put (dico )
323322
324- # TODO: Better error handling
325- logger .debug ("Awaiting application..." )
326- await application (scope , receive , send )
323+ # TODO: Better error handling
324+ logger .debug ("Awaiting application..." )
325+ await application (scope , receive , send )
327326
328- logger .debug ("Waiting for headers" )
329- headers : dict
330- if scope ["type" ] == "http" :
331- headers = await send_queue .get ()
332- else :
333- headers = {}
327+ logger .debug ("Waiting for headers" )
328+ headers : dict
329+ if scope ["type" ] == "http" :
330+ headers = await send_queue .get ()
331+ else :
332+ headers = {}
333+
334+ logger .debug ("Waiting for body" )
335+ response_body : dict = await send_queue .get ()
334336
335- logger .debug ("Waiting for body" )
336- response_body : dict = await send_queue .get ()
337+ logger .debug ("Waiting for buffer" )
337338
338- logger .debug ("Waiting for buffer " )
339- output = buf . getvalue ( )
339+ logger .debug (f"Headers { headers } " )
340+ logger . debug ( f"Body { response_body } " )
340341
341- logger . debug ( f"Headers { headers } " )
342- logger . debug ( f"Body { response_body } " )
343- logger . debug ( f"Output { output } " )
342+ # Since Python code runs asynchronously in the same process, sharing the global sys.stdout, prints from an
343+ # individual call cannot be isolated from other calls.
344+ output = ""
344345
345346 logger .debug ("Getting output data" )
346347 output_data : bytes
0 commit comments