Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77985

Re: the python shell window is already executing a command

From Terry Reedy <tjreedy@udel.edu>
Subject Re: the python shell window is already executing a command
Date 2014-09-17 18:56 -0400
References <4jrh1adtbsvnr9o20r9k3hfe4nkrkps2dk@4ax.com> <mailman.14068.1410940978.18130.python-list@python.org> <lsaj1a5pm5k301k9ipn6fcsra5c776bs1q@4ax.com>
Newsgroups comp.lang.python
Message-ID <mailman.14090.1410994642.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 9/17/2014 11:55 AM, Seymore4Head wrote:
> On Wed, 17 Sep 2014 04:02:16 -0400, Terry Reedy <tjreedy@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

the python shell window is already executing a command Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-16 22:17 -0400
  Re: the python shell window is already executing a command Terry Reedy <tjreedy@udel.edu> - 2014-09-17 04:02 -0400
    Re: the python shell window is already executing a command Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-17 11:55 -0400
      Re: the python shell window is already executing a command Terry Reedy <tjreedy@udel.edu> - 2014-09-17 18:56 -0400
        Re: the python shell window is already executing a command Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-17 21:34 -0400
          Re: the python shell window is already executing a command Terry Reedy <tjreedy@udel.edu> - 2014-09-17 23:50 -0400
            Re: the python shell window is already executing a command Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-18 11:24 -0400
              Re: the python shell window is already executing a command Terry Reedy <tjreedy@udel.edu> - 2014-09-18 15:05 -0400
                Re: the python shell window is already executing a command Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-18 15:58 -0400
              Re: the python shell window is already executing a command Chris Angelico <rosuav@gmail.com> - 2014-09-19 12:51 +1000
              Re: the python shell window is already executing a command Terry Reedy <tjreedy@udel.edu> - 2014-09-19 15:43 -0400
              Re: the python shell window is already executing a command Chris Angelico <rosuav@gmail.com> - 2014-09-20 05:47 +1000
  Re: the python shell window is already executing a command Tony the Tiger <tony@tiger.invalid> - 2014-09-18 16:50 +0000

csiph-web