473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Programmatically exit the REPL

I've got a pretty complex interactive command line program. Instead of
writing my own REPL, I'm using the Python interpreter (an infinitely
better solution). This program has two threads, a background thread and
the REPL thread. When you call quit() or sys.exit() in the REPL thread,
everything is perfectly happy. However, the background thread does some
long-running jobs, and I want it to have the ability to exit the program
when the job is complete. When I call quit() or sys.exit() from the
background thread, the REPL merrily continues on its way.

This is a very frustrating problem, so I'm hoping someone can shed some
light on it. Am I missing something simple? Or is this just impossible?
I don't see anything about breaking out of interact() in the code module
docs.
Here's a minimal example:

#!/usr/bin/env python -i
# You get the same behavior using code.interact()

import sys
import time
import threading

def end_the_program():
# works if you call it from the REPL thread,
# but not the background thread
print "called end_the_program()"
sys.exit()
# quit() # using quit() rather than sys.exit()
# results in identical behavior

keep_going = True
def runner():
while keep_going:
time.sleep(0.1)
end_the_program()
threading.Thread(target=runner).start()

# end example
Here's the console session (edited for clarity):

Desktop$ ./exit_repl.py
>>keep_going = False
called end_the_program()
# notice we didn't exit here
>>end_the_program()
called end_the_program()
# but we did exit here
Desktop$
-Matt
Aug 26 '08 #1
3 1602
Without reading your post properly or having tried to do the same thing
myself: I think you might want to have a look at ipython; it gives a better
REPL and "embedding ipython" should give you plenty of hits as well.

Matthew Fitzgibbons <el*****@nienna.orgwrites:
I've got a pretty complex interactive command line program. Instead of writing
my own REPL, I'm using the Python interpreter (an infinitely better solution).
This program has two threads, a background thread and the REPL thread. When
you call quit() or sys.exit() in the REPL thread, everything is perfectly
happy. However, the background thread does some long-running jobs, and I want
it to have the ability to exit the program when the job is complete. When I
call quit() or sys.exit() from the background thread, the REPL merrily
continues on its way.

This is a very frustrating problem, so I'm hoping someone can shed some light
on it. Am I missing something simple? Or is this just impossible? I don't see
anything about breaking out of interact() in the code module docs.
Here's a minimal example:

#!/usr/bin/env python -i
# You get the same behavior using code.interact()

import sys
import time
import threading

def end_the_program():
# works if you call it from the REPL thread,
# but not the background thread
print "called end_the_program()"
sys.exit()
# quit() # using quit() rather than sys.exit()
# results in identical behavior

keep_going = True
def runner():
while keep_going:
time.sleep(0.1)
end_the_program()
threading.Thread(target=runner).start()

# end example
Here's the console session (edited for clarity):

Desktop$ ./exit_repl.py
>>>keep_going = False
called end_the_program()
# notice we didn't exit here
>>>end_the_program()
called end_the_program()
# but we did exit here
Desktop$
-Matt
--
Aug 26 '08 #2
Alexander Schmolck wrote:
Without reading your post properly or having tried to do the same thing
myself: I think you might want to have a look at ipython; it gives a better
REPL and "embedding ipython" should give you plenty of hits as well.
Thanks for the tip; I hadn't heard of ipython before. I will certainly
check it out. However, if anyone knows how to break out of the
interactive interpreter from another thread, I'd still very much like to
hear about it. :)

-Matt
Aug 26 '08 #3
Almar Klein wrote:
Hi,

If you insist on writing your own shell, you can also consider running
the commands in another python process.
I took the source code of Pype as an example, which uses a wx.Process.
I've tried the subprocess module as well, but could not get it to work.

Almar

2008/8/26 Matthew Fitzgibbons <el*****@nienna.org
<mailto:el*****@nienna.org>>

Alexander Schmolck wrote:

Without reading your post properly or having tried to do the
same thing
myself: I think you might want to have a look at ipython; it
gives a better
REPL and "embedding ipython" should give you plenty of hits as well.
Thanks for the tip; I hadn't heard of ipython before. I will
certainly check it out. However, if anyone knows how to break out of
the interactive interpreter from another thread, I'd still very much
like to hear about it. :)
-Matt
--
http://mail.python.org/mailman/listinfo/python-list

The whole point is that I *don't* want to write my own shell, I want to
use the Python interactive shell (iPython, while very neat, is overkill
for this purpose, and an extra dependency).

I use subprocess regularly, but it doesn't work in this case because (a)
the point of using an interactive interpreter is so that I can interact
with the job *while it's running* and (b) I *still* wouldn't be able
exit the interactive shell cleanly (I'd have to look up the PID and kill
it via the shell -- very ugly and not portable).

-Matt
Aug 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: StevePBurgess | last post by:
I would like to make my downloads section unbrowsable (to users) but accessible to scripts. Can I deliver a file to a browser without linking to it's URL so that I can deliver files...
4
by: u7djo | last post by:
Hi, I'm currently building an application in Access and as part of this need to import forms and modules from another database. Some of the imports will be revisions of existing forms/modules so I...
2
by: VR | last post by:
Please, help. Is there any way to take advantage of re-ordering columns in ListView control programmatically when AllowColumnReorder is set to true? I have a ListView with 3 columns,...
4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
2
by: Dennis | last post by:
Looking for a method to programmatically convert (using vB) a Word doc to it's ASCII equivalent. I understand I'll lose all formatting, and that's okay Anyone out there have ideas Thaks ...
32
by: deko | last post by:
I have a popup form with a textbox that is bound to a memo field. I've been warned about memo fields so I'm wondering if I should use this code. Is there any risk with changing the form's...
25
by: bubbles | last post by:
Using Access 2003 front-end, with SQL Server 2005 backend. I need to make the front-end application automatically refresh the linked SQL Server tables. New tables will be added dynamically in...
0
by: PeterSchwennesen | last post by:
Problems starting a Timer Programmatically within a BackgroundWorker. I am trying to start a Timer inside a Backgroundworker. I want to start the BackGroundWorker and then have a timer tick a...
0
by: Matthew Fitzgibbons | last post by:
Matthew Fitzgibbons wrote: Here's a modified example that _almost_ works: #!/usr/bin/env python import code import time import threading
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.