[f2py] Re: allocatable arrays & threads

Johnny Lin jlin at northpark.edu
Thu Aug 14 18:53:14 EEST 2008


Hi Fred,

This doesn't address your specific question about threads, but
describes a workaround I did for something similar.  I'm mailing
the list with my reply, in case someone has a comment on whether
my workaround is a bad idea.

I've got an extension module that has a global memory space that
stores variables.  I created a class that imports that module.  I
want the imported extension module associated for each instance of
the class, however, to have a separate memory space from other
instances, so each instance would access only its version of a given
variable in that extension module's memory space.

The only way I could think of to get what I wanted was to create a
temporary directory, copy the shared object file to that directory,
and import that copied version of the extension module.  Here's the
code that does it, along with a check at the end that you imported
the copied version (origsofile is the installed location of the
extension module's shared object file, self.sodir is the name of
the temporary directory, created using the tempfile module, soname
is the extension module's name, and the imported extension module
is stored as the attribute self.__qtcm):

        origsofile = \
            os.path.join( os.path.split(_package_version.__file__)[0] \
                        , soname + '.so' )
        shutil.copy2(origsofile, self.sodir)
        sys.path.insert(0, self.sodir)
        self.__qtcm = __import__(soname)
        sys.path.remove(self.sodir)
        del sys.modules[soname]

        if os.path.split(self.__qtcm.__file__)[0] != self.sodir:
            raise ValueError, 'Incorrect import of .so library'

In your case, maybe your first and second threads are accessing the
same variable in memory, i.e., each thread doesn't allocate its own
array but accesses the variable in the extension module in whatever
state it is in?  I'd also very much welcome being corrected.

Thanks!

Best,
-Johnny


On Thu, Aug 14, 2008 at 04:14:04PM +0200, fred wrote:
> fred a ?crit :
>> Hi,
>>
>> I want to use python threads in my code.
>>
>> The thread is a call to a Fortran function.
> Hi all,
>
> No news about my issue ?
>
> No one can help me ?
>
> I'm really stucka and need some help.
>
>
> Cheers,
>
> -- 
> Fred
>
> _______________________________________________
> f2py-users mailing list
> f2py-users at cens.ioc.ee
> http://cens.ioc.ee/mailman/listinfo/f2py-users

-- 

Johnny Wei-Bing Lin
Associate Professor of Physics

North Park University
(773) 244-6266 phone
jlin at northpark.edu
www.johnny-lin.com



More information about the f2py-users mailing list