I am trying to use ctypes and load the same compiled Fortran library twice, such that I have two independent instances of it, such that any module variables that the library contains are not stored in the same memory locations. The general solution described (for example, here: https://mail.python.org/pipermail/python-list/2010-May/575368.html) is to provide the full path to the library instead of just its name. However, I am not able to get this to work like this. Here is a minimal working example that demonstrates the problem:
test.f90:
module test
use iso_c_binding, only: c_int
implicit none
integer :: n
contains
integer(c_int) function get() bind(c, name='get')
get = n
end function get
subroutine set(new_n) bind(c, name='set')
integer(c_int), intent(in) :: new_n
n = new_n
end subroutine set
end module test
test.py:
import os
from ctypes import cdll, c_int, byref
if __name__ == '__main__':
lib1 = cdll.LoadLibrary(os.path.abspath('test.so'))
lib2 = cdll.LoadLibrary(os.path.abspath('test.so'))
lib1.set(byref(c_int(0)))
lib2.set(byref(c_int(1)))
print(lib1.get())
The Fortran library is compiled using the command:
gfortran -shared -fPIC -o test.so test.f90
When I run python test.py I get 1 as output, while I want to get 0. Does anyone know how to make this work?