the python shell window is already executing a command

Terry Reedy tjreedy at udel.edu
Wed Sep 17 18:56:47 EDT 2014


On 9/17/2014 11:55 AM, Seymore4Head wrote:
> On Wed, 17 Sep 2014 04:02:16 -0400, Terry Reedy <tjreedy at udel.edu>
> wrote:
>> On 9/16/2014 10:17 PM, Seymore4Head wrote:
>>> What I do is click on the IDLE window and without making any changes I
>>> just hit f5 to rerun the program.
>>> Sometimes I get the error "the python shell window is already
>>> executing a command" and sometimes not.

You left out an important part of the error message ""please wait until 
it is finished."

>>> I am using XP and Python 3.4.1.

I am using 3.4.1 on Win 7.

>>> Is there a way to rerun a program without getting this error?

Follow the instruction you were given, but omitted, or see below.

>> Normally, hitting f5 kills the previous process if it is still running
>> and starts the one in the editor.  Please post a minimal program that
>> exhibits the problem.
>
> It happens in almost every program I have written.  I just tried this
> one again.  I could run the program by pushing f5.  The first couple
> of times it would run again by switching back to IDLE and pressing f5,
> but after the second or third time, it gives an error that the python
> shell window is already executing a command.
>
> If I make a change to the program, like adding or deleting a 0 from
> "rounds" the program will run without generating an error, but if I
> try to re run the program without changing anything, I get the error
> almost every time.
>
>
> Here is one.
>
> import random
> count =0
> rounds=1000
> heads=0
> tails=0
> ht=[0,1]
> while count<rounds:
>      coin=random.choice(ht)
>      if coin == 0:
>          heads=heads+1
>      elif coin == 1:
>          tails=tails+1
>      count = count + 1
> print (heads,tails)
> print ('Heads {:.2%} Tails "{:.2%} "'.format(heads/rounds,
> tails/rounds))

I am unable to reproduce the problem. When I run this program from an 
Idle editor, it finished before I can click on the Editor window and hit 
F5 again.  The same remains true with 1 or 2 zeros added.  With 1000000 
rounds, I get the expected behavior, which is no ouput from the 
cancelled process and a clean restart.

A little digging with Idle's grep (Find in Files) shows that the message 
is produced by this code in idlelib/PyShell.py, about 825.

     def display_executing_dialog(self):
         tkMessageBox.showerror(
             "Already executing",
             "The Python Shell window is already executing a command; "
             "please wait until it is finished.",
             master=self.tkconsole.text)

This function is only called here (about line 735)
     def runcommand(self, code):
         "Run the code without invoking the debugger"
         # The code better not raise an exception!
         if self.tkconsole.executing:
             self.display_executing_dialog()
         <else run idle code in user process output view user>

How is this run?  Run-Module F5 invokes 
ScriptBinding.run_module_event(116) and thence _run_module_event (129). 
This methods includes this.
         if PyShell.use_subprocess:
             interp.restart_subprocess(with_cwd=False)

restart_subprocess includes these lines (starting at 470):
         # Kill subprocess, spawn a new one, accept connection.
         self.rpcclt.close()
         self.terminate_subprocess()
         console = self.tkconsole
         ...
         console.executing = False  # == self.tkconsole
         ...
         self.transfer_path(with_cwd=with_cwd)

transfer_path calls runcommand but only after tkconsole.executing has 
been set to False.  But this only happens if PyShell.use_subprocess is 
True, which it normally is, but not if one starts Idle with the -n option.

After conditionally calling interp.restart_subprocess, _run_module_event 
directly calls interp.runcommand, which can fail when running with -n. 
Are you?  This is the only way I know to get the error message.  Is so, 
the second way to not get the error message is to not use -n and run 
normally.

-- 
Terry Jan Reedy





More information about the Python-list mailing list