473,396 Members | 1,861 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,396 software developers and data experts.

Re: Programmatically exit the REPL

Matthew Fitzgibbons wrote:
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
Here's a modified example that _almost_ works:

#!/usr/bin/env python

import code
import time
import threading

keep_going = True
def runner():
while keep_going:
time.sleep(0.1)
ic.push("quit()") # this will exit the InteractiveConsole loop!
threading.Thread(target=runner).start()

ic = code.InteractiveConsole(locals())
ic.interact("InteractiveConsole")

# end example
It uses code.InteractiveConsole instead of python -i. When you push
"quit()" to the InteractiveConsole, the loop exits, but the raw_input
call is still blocking. Argh!

Console session:

Desktop$ ./exit_repl.py && echo done
InteractiveConsole
>>keep_going = False
<hit enter one more time...>
done
Desktop$
Does anyone know how to make raw_input think it has gotten input?

-Matt
Aug 26 '08 #1
0 1111

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...
3
by: Matthew Fitzgibbons | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.