My app is single threaded, so this may work. I'll give it a shot.

2009/1/5 Steve McConville <[email protected]>:
>
> Don't have time to test it, but maybe a simpler approach would be to
> explicitly replace the default SIGINT handler with something that
> doesn't throw a KeyboardInterrupt? I believe it's possible to register
> a signal handler that will intercept them asynchronously, something
> like
>
>
>
> import signal
> signal.signal(signal.SIGINT, menu)
>
> def code():
>    ...
>
> def menu():
>    # raise KeyboardError
>    do_useful_things_instead()
>
> while not_done:
>    x.code()
>
>
>
> This is probably un-threadsafe in a zillion ways though, but see if it
> works. I hate threads anyway.
>
> http://docs.python.org/library/signal.html
>
> 2009/1/5 Padraig Kitterick <[email protected]>:
>>
>> Daniel Kersten wrote:
>>> My code is something like this:
>>>
>>> while not_done:
>>>     try:
>>>         code()
>>>     except keyboardinterupt:
>>>         print "Menu:"
>>>         print " debug    - run pdb"
>>>         print " resume  - resume program"
>>>         print " abort      - abort program"
>>>         ...
>>>         command = raw_input("> ")
>>>         if command == "debug":
>>>             pdb.set_trace()
>>>         ...
>>>
>>> The idea is that code() is run in a loop executing data (in my case, a
>>> single entry to a log file) and Ctrl-C pauses the program and pops up
>>> a little menu. The problem is that in certain cases, code() may have
>>> already altered data and breaking out and rerunning it will either 1)
>>> lose data or 2) duplicate data.
>>>
>>> The only real solution I can think of is catching the interrupt in a
>>> separate thread which sets a flag telling the main thread to run pdb,
>>> or abort or whatever, but ideally, I'd like to catch the exception and
>>> somehow magically resume.
>>>
>>>
>>> 2009/1/5 Ajurna <[email protected]>:
>>>
>>>> hi,
>>>>     it might be me and im getting this wrong but surely you could use a 
>>>> try:
>>>> except clause?
>>>>
>>>> try:
>>>>     codeloop()
>>>> except keyboardinterupt:
>>>>     mainloop()
>>>>
>>>>
>>>> or something to that effect.
>>>>
>>>> On Mon, Jan 5, 2009 at 3:38 PM, Daniel Kersten <[email protected]> wrote:
>>>>
>>>>> Hi & happy new year,
>>>>>
>>>>> I'm wondering if theres anyway of resuming after a KeyboardInterrupt
>>>>> exception, if I catch it. For example, can I retrieve the code object
>>>>> or stack or something from when execution was interrupted and continue
>>>>> from there.
>>>>>
>>>>> Basically, I would like to use Ctrl-C as a way to temporarily break
>>>>> out of my program to enter a menu (in this particular case, the menu
>>>>> allows you to quit, continue and enter pdb). This works fine in most
>>>>> cases, but if I interrupt in the middle of certain operations, when I
>>>>> resume, I lose data. Of course, I can write my code to avoid this, but
>>>>> that may not always be possible.
>>>>>
>>>>> Any ideas? Or do I need to handle the menu in a separate thread..?
>>>>>
>>>>> Thanks!
>>>>> Dan.
>>>>>
>>>>> --
>>>>> Daniel Kersten.
>>>>> Leveraging dynamic paradigms since the synergies of 1985.
>>>>>
>>>>>
>>>>>
>>>>
>>>> --
>>>> ~Ajurna
>>>>
>>>>
>>>
>>>
>>>
>>>
>> AFAIK this isn't really possible to do in Python [1], as it doesn't have
>> the ability to restart after an exception is raised, such as is possible
>> in LISP [2]. You can try to constantly store the current state of all
>> objects to implement a support for a kind of resume, but that would mean
>> altering any code extensively.
>>
>> [1] http://mail.python.org/pipermail/python-list/2005-June/327048.html
>> [2]
>> http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html
>>
>> >
>>
>
>
>
> --
> steev
> http://www.daikaiju.org.uk/~steve/
>
> >
>



-- 
Daniel Kersten.
Leveraging dynamic paradigms since the synergies of 1985.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Python Ireland" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.ie/group/pythonireland?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to