get reference to current InteractiveShell instance in IPython (trying to hook %exit) - ipython

I'd like to create a hook in IPython that runs when i do Cntl-D, %Exit, %Quit, or %exit and %quit and confirm.
The motivation is that i want to have a thread that runs in IPython indefinitely in the background until i press Cntl-D to exit, and then i want to tell that thread to quit. I figure that i will use the hook to send a signal to my background thread.
I figure one way to do this would be to wrap IPython::iplib::InteractiveShell::exit. Is this possible? Is there a better way to do what i am trying to do? thanks

The easiest way to tell your thread to exit is probably to register an atexit handler. That's a standard Python thing, that doesn't depend on IPython.
If you want a reference to the InteratctiveShell object, in IPython 0.11+ use get_ipython(). I think you're using an older version, though; try _ip.

Related

What code should I use? sys.exit() or some other thing I haven't found?

I'm a high school student in a class that is using Python 2 on Enthought Canopy Windows Edition. I often get stuck in coding loops, but the only way I can get out is by closing out all forms of open Enthought. Is there any more reliable way to do it? I've been using sys.exit(0), but it doesn't work correctly, and is leaving my coding in a jam. I can't even test it without it getting me stuck. Anyone know how to fix this issue?
If your code is already running, then you can't reliably type a command to stop it, because the prompt won't necessarily be live then.
Often you can use the Run menu's "Interrupt kernel" command, and you should usually be able to use "Restart kernel", though this doesn't work all the time.

How can I know whether process has a windows or not?

Sometimes my program is finished incorrectly. GUI is gone but processes still present in the system. So I need to verify whether the program finishes correctly or not by checking GUI presence. Could anyone help me with it?
It looks like what you need is WASP.
You can use WASP's Select-Window to list all window handles.
You can then use Select-Window Myapp* to set focus on the window, if required.

Calling elisp code when awakening from sleep

I'd like to configure emacs a little differently when coming in over remote desktop. I can detect the rdp session, but I'd like to automatically run the function that checks when emacs wakes from sleep. I believe Windows issues a PBT_APMRESUMESUSPEND event when awakening because of user activity -- is there a way to hook this from within emacs?
This would be for emacs 24.4 on Windows. Some code or a pointer to the right documentation would be great. I've looked but am not seeing anything -- maybe I'm not looking in the right place. Thanks in advance.
I don't know of any hooks that are triggered in your situation, would it be possible to solve your situation using the input focus hooks?
Specifically, focus-in-hook.

Changing Code At Runtime While Debugging

I am using Eclipse Kepler Service Release 2 , EPIC 0.5.46 and Strawberry Perl 5 version 18 for perl programming. For debugging I am using Eclipse debugger and PadWalker .
I have an interactive perl program that writes to files based on answers provided by the users to multiple prompts. While debugging , every time i change a single line of code I have to rerun the whole program again and provide inputs to every prompt , which is really time consuming.
Is there a way to make changes to the code in a sub routine , in the middle of debugging session such that the instruction pointer resets itself to the first line of that sub routine. This way i do not have to restart the session to recompile the new code.
Appreciate your inputs and suggestions. Thank You!!!
What you want to do can be done, and I've done it many times in Perl myself. For example, see this.
However although what you describe may work (and is a bit dangerous), the way it is generally done a bit different and safer.
First one has to assume a regular kind of command structure like a command processor, or say a web server.
In a command processor or web server, you read a command (or get a web request), perform an action, then read another command, perform another action and so on. From your description, it sounds like you have such a structure.
In my case, I have each debugger command stored as in Perl file. This is helpful not only for facilitating this task, but also for understanding, testing and changing the code.
Given this kind of program structure, instead of trying to change the program counter, you complete the command and at the level where you are about to read a new command, you make the change and then reload the file which changes the code.
The specific Perl construct to do this is called do. Don't use require or use which will load in a Perl file only if that file or module hasn't been previously loaded. In your situation, you want to reload even if it has been loaded before.
So now how do you get to be able to issue a do command? As you suggest, you could do it through a debugger. Assuming you have this overall program stucture as described above, you put the breakpoint somewhere a common point in the caller which loops over things to process, rather than try to change things in indvidual commands.
And you don't even need a debugger to do this! Many web frameworks like Ruby on Rails, have a "development" mode where they save timestamps on files that implement functionality. If the file has changed they issue the "do" command before running the request.

Lua and os.execute return

I am writing a lua script and I have to execute two shell commands that both keep echo-ing information until terminated with ^C.
This means os.execute is useless since it waits for the return code, which never comes, and freezes the entire script. Do you have any idea on how to make this work? A good solution is not to require os.execute to return any value so it will send the command and move on but I think this is not possible. Another is multithreading that I haven't been able to make it work whatsoever.
I also must have the ability to somehow stop both infinite-loops by either using ^C or a lua method. The script is running on iPhone (iOS 5.0.1 / root) using Lua 5.1.4.
os.execute('yourcommand&')
That should run the command in the background and return to your Lua script immediately.
If you want more sophisticated process control, you're probably going to want to write that in native code.