Re: [f2py] Re: unloading/reloading compiled modules

Demaeyer Jonathan <[email protected]>
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
Hi everyone,

I have the same problem.
I'm a physicist working in statistical mechanics.

For the moment, I try to develop a front end for my various pieces of
code. To speed up some parts I use f2py. This approach was very
effective (thank you to the developer of f2py) but I have some troubles
now :

The user can specify a mathematical function that the program transforms
into a fortran module to be used by the other subroutines.

If the user changes the function, the program rewrites the module and
recalls f2py compilation.
The problem is that when the program reloads the module, nothing seems
to change, the same function as previously is still used.

I suppose the best way to tackle this problem is to make a python
function and give it to the wrapped fortran subroutines, but I'm
worrying about speed issues.
Thank you in advance for yours comments,

Jonathan

Kevin Mitchell a écrit :
> I tried sending this before I was a subscriber and I'm still waiting
> for approval. So apologies if its out of line to be sending again, but
> I wanted to get this out there in case anyone had any ideas.
> 
> There was a thread on the numpy-discussion list about this, but its really old,
> http://www.mail-archive.com/numpy-discussion-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org/msg02661.html
> 
> Basically, I want to be able to reload a recompiled Fortran module
> without restarting python. This is more or less essential for using
> python as a development interface.
> 
> I understand that what really needs to happen is for the old version
> of the module to first get UNloaded. To this end, I put together the
> following script based on what I found at
> http://stackoverflow.com/questions/359498/how-can-i-unload-a-dll-using-ctypes-in-python
> 
> -------------------------------------
> #!/usr/bin/env python
> import os
> import ctypes
> import numpy.f2py
> 
> def isloaded(lib):
>    libp = os.path.abspath(lib)
>    fd=open('/proc/%d/maps'%os.getpid())
>    ret=fd.read().find(libp)
>    fd.close()
>    return (ret != -1)
> 
> def dlclose(handle):
>    libdl = ctypes.CDLL("libdl.so")
>    libdl.dlclose(handle)
> 
> def unload(fname):
>    mydll = ctypes.CDLL(fname)
>    handle = mydll._handle
>    del mydll
>    while isloaded(fname):
>        dlclose(handle)
> 
> import yo
> 
> yo.yo()
> 
> unload('./yo.so')
> 
> print 'isloaded(\'./yo.so\') =', isloaded('./yo.so')
> 
> import yo
> 
> print 'good to here!'
> 
> yo.yo()
> 
> print 'you\'ll never see this'
> ----------------------------------------------------------
> 
> Where "yo.so" is compiled from yo.f:
> ---------------------------
>      subroutine yo()
>      write(*,*) 'yo'
>      end
> ----------------------------
> with
> 
> f2py -c yo.f -m yo
> 
> It segfaults at the second calling of yo.yo(). In fact, it segfaults
> on exit even if I never reload the module. My guess would be that
> something is expecting that library to still be linked, but I have no
> idea where to look or how to change that expectation. If its not
> already obvious from the above script, I should probably mention that
> I'm using Linux and I could care less about platform portability.
> 
> Kevin
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.