python - How to see print output from generator before ending the cycle? -


i'm trying print debug information inside generator working big list of data. but, can see result when generator finishes.

i using python 3 , code follows:

def generator():     while 1:         print ('.', end='')         time.sleep(1)         yield 1  in generator():     print ('|', end='') 

result:

^c.|.|.|.|.| 

equivalent php7 code works expected:

function generator() {     while (1) {         echo '.';         sleep(1);         yield 1;     } }  foreach (generator() $item) {     echo '|'; } 

result:

.|.|.|.|.|^c 

how print debug information in realtime each iteration of generator's cycle?

tl;dr:

i believe have similar issue question: print statements not working when serve_forever() called? (although title not apparent...)

try flush prints:

print ('.', end='', flush=true)  print ('|', end='', flush=true) 

flush

the method flush() flushes internal buffer, stdio's fflush. may no-op on file-like objects.

it forces print() function print whatever have been buffered point stdout of machine.

usually needed on windows operating system.

good luck :)


Comments

Popular posts from this blog

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to implement an entity bound odata action in olingo v4.3 -