Re: [f2py] cancel fortran module inside python shell

Mark Andrews <[email protected]> Sat, 8 Jan 2011 18:41:47 +0000
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
looks very useful. thanks.
-m

On Thu, Jan 6, 2011 at 11:21 AM, James Kermode <[email protected]> wrote:
> If you're not too worried about memory leaks, it's possible to slightly modify f2py to allow interrupts with Ctrl+C to be caught and turned into Python exceptions. I've done this by changing the code generated by f2py to call the fortran routine so that it installs a custom signal handler for the SIGINT signal. A setjmp()/longjmp() trap is used to raise an exception if an interrupt occurs inside the fortran routine. This technique also allows Fortran routines to raise exceptions if an error occurs. You can see the code I use to do this at the links below.
>
> James.
>
> http://src.tcm.phy.cam.ac.uk/viewvc/jrk33/repo/trunk/QUIP/Tools/quippy/patch_f2py.py?revision=7798&view=co
> http://src.tcm.phy.cam.ac.uk/viewvc/jrk33/repo/trunk/QUIP/libAtoms/cutil.c?view=co
>
> On 5 Jan 2011, at 20:02, Kevin Mitchell wrote:
>
>> As far as I know there is no good way to implement this. The problem
>> is that the Fortran code is called by the process running the python
>> shell. Therefore the two cannot be distinguished at the operating
>> system level. That said, it would be dangerous if python actually
>> heeded your request and ceased running the module midway through
>> because it might cause memory leaks or undefined behaviour since there
>> is no way for python to know what Fortran is actually doing so that it
>> can stop at an opportune time.
>>
>> On 1/5/11, Mark Andrews <[email protected]> wrote:
>>> Hi,
>>> I have a simple question, sorry if the answer is out there and my
>>> googling missed it.
>>> To cancel or interrupt a running python script inside a python shell
>>> like e.g., ipython, I press 'ctrl+c'.
>>> If I write a pure fortran program and run it from a linux bash shell,
>>> I can also cancel by pressing "ctrl+c".
>>> However, if I write a fortran module for python using f2py and run
>>> that in a python shell, I can not cancel with "ctrl+c". The only way I
>>> can interrupt it is (with linux) issuing a kill command to the entire
>>> python shell process.
>>> I was wondering if there is something I am missing? Is there something
>>> I should add to the fortran code or maybe to the f2py command line
>>> flags?
>>> thanks,
>>> Mark
>>>
>>> _______________________________________________
>>> f2py-users mailing list
>>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>>>
>>
>> _______________________________________________
>> f2py-users mailing list
>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
> --
> Dr James Kermode, Research Associate
> Department of Physics, King's College London
> Room: Strand 7.33, Tel: +44 (0)20 7848 2064
> http://www.jrkermode.co.uk
>
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>