engineer-man / piston

A high performance general purpose code execution engine.
https://emkc.org/run
MIT License
1.95k stars 251 forks source link

In socket mode, python outputs come delayed and not immediate #682

Closed RahulBadenkal closed 2 months ago

RahulBadenkal commented 2 months ago

When i execute the below python program in socket mode (via /connect)

from time import sleep

sleep(3)
print('1')

sleep(3)
print('2')

sleep(3)
print('3')

The output all comes at once, rather than at 3 sec intervals

RahulBadenkal commented 2 months ago

Looks like adding this env variable PYTHONUNBUFFERED = 1 makes stdout/stdin streams to be unbuffered. Reference

Adding -u flag to request doesn't work as we need -u <file-name> but in the api app the args are set the other way <file-name ...flags> Reference

Possible to add PYTHONUNBUFFERED = 1 to the package env file of python? https://github.com/engineer-man/piston/blob/master/packages/python/3.12.0/environment

Brikaa commented 2 months ago

For now, you can use print('1', flush=True). I think this should cause the output to appear immediately.

RahulBadenkal commented 2 months ago

While what you mentioned works, it would be better if it was added as part of the env file so the user won't have be informed to set flush while executing code.

For now I have forked the repo and added a post build script that installs python package and updates the env file.