-1

I'm trying to make a game engine using the python command line but sys.stdout.write and print('',end='') bog down the command line and crash it.

I've tried stdout and print's end keyword argument, but they crash the command line. What's going on?

This code using print without the end argument works fine:

while True:
    print('                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         =                                                                              ===                                                                            =====                                                                          =======                                                                        =========                                                                      ===========                                                                    =============    YYYYYYYYYYY                                                 ================================================================================')

While this, which uses sys.stdout.write to prevent the new line from printing, has problems:

import sys
while True:
    sys.stdout.write('                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         =                                                                              ===                                                                            =====                                                                          =======                                                                        =========                                                                      ===========                                                                    =============    YYYYYYYYYYY                                                 ================================================================================')
    sys.stdout.flush()

I expect it to run fast with print and end or sys.stdout, but instead the console bogs down and crashes.

martineau
  • 119,623
  • 25
  • 170
  • 301
KillerQuow
  • 37
  • 8

1 Answers1

3

You're using the IDLE shell to run your program, don't.

IDLE poorly manages outputting text and does slow down when outputting extremely long lines of it (this is the reason why print without the end keyword argument works alright, because you're printing multiple separate lines, not 1 line which is getting increasingly longer).

Instead just use the standard Windows command line (cmd.exe).

You can do this (assuming your installation has been configured correctly) by either running your .py file via the GUI or by running:

python filename.py

in the command line.

This has been reported as a bug in Python (IDLE shell window gets very slow when displaying long lines) back in 2006 and the issue is still open.

You can read more including some numbers on this Stack Overflow post.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
  • thanks. doesn't windows cmd window write past the edge of the screen though? I am printing the entire frame to the screen at a time and that would screw everything up. – KillerQuow Jun 25 '19 at 16:58
  • @KillerQuow No, however it does have an adjustable width, you just need to resize the window appropriately for it to display the way intended – Nick is tired Jun 25 '19 at 16:59
  • Why does the print command without the end attribute work fine then? – KillerQuow Jun 25 '19 at 17:08
  • @KillerQuow Updated the answer: this is the reason why print without the end keyword argument works alright, because you're printing multiple separate lines, not 1 line which is getting increasingly longer – Nick is tired Jun 25 '19 at 17:10
  • But why hasn't it been fixed if it's been known since 2006? – KillerQuow Jun 26 '19 at 01:20
  • 1
    python.exe is a console application, which means that by default it either inherits a console from the parent or allocates a new console if the parent doesn't have one. A console is a system resource that's hosted by an instance of conhost.exe. We commonly start a console session with a shell such as cmd.exe or powershell.exe, but we can run python.exe directly from a shortcut or the Win+R menu, and get a new console that's not inherited from a shell. So please try to not confuse the console with cmd.exe. They aren't even remotely the same thing. – Eryk Sun Jun 26 '19 at 04:41
  • @eryksun I didn't say run it in the console, I said that instead of using the IDLE shell use cmd.exe (it's *common name* being the command line), this clearly implies I'm referring to it as a shell – Nick is tired Jun 26 '19 at 08:03
  • @NickA, the OP said "doesn't windows cmd window write past the edge of the screen", to which you responded "no, however it does have an adjustable width". You're both actually talking about the console window that's hosted by conhost.exe, to which python.exe is attached, and which has nothing inherently to do with "cmd". – Eryk Sun Jun 26 '19 at 08:50