-1

I tried to call python code from c, the example runs ok for sample code on my environment(python3.6), but when I integrate it into my program, I got following error when I call Py_Initialize();:

...
sem_init: Success
Fatal Python error: Can't initialize threads for interpreter

Could you provide some clues to solve this problem?

It seems the error comes from here, but I am still not sure how to avoid this.

wangzhe
  • 573
  • 1
  • 5
  • 13
  • Did you try debugging it? You found the place where the error happens. Now you need to examine the state of the program at this point to see which value is wrong, then track it to its origin. – ivan_pozdeev Jan 13 '19 at 18:09
  • I tried gdb, but I don't have the source file of the python, some error such as `Py_Initialize () at Python/pylifecycle.c:480 480 Python/pylifecycle.c: No such file or directory. (gdb) n` – wangzhe Jan 13 '19 at 18:11
  • You need to get the corresponding sources then. They usually come in "source packages" (this depends on your distribution). See e.g. https://askubuntu.com/questions/28372/how-do-i-get-and-modify-the-source-code-of-packages-installed-through-apt-get – ivan_pozdeev Jan 13 '19 at 18:17
  • it seems I need extra permission to do this, thanks anyway. – wangzhe Jan 13 '19 at 18:19
  • Possible duplicate of [What should I do if two libraries provide a function with the same name generating a conflict?](https://stackoverflow.com/questions/678254/what-should-i-do-if-two-libraries-provide-a-function-with-the-same-name-generati) – ivan_pozdeev Jan 28 '19 at 05:26

2 Answers2

0

The failing code is

    if (head_mutex == NULL)
        Py_FatalError("Can't initialize threads for interpreter");

Searching the code back for head_mutex references finds

#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))

which is called right before the failing code.

So, the reason is that PyThread_allocate_lock returns NULL. There are a few different implementations for it in Python codebase depending on the OS and build flags, so you need to debug it or otherwise figure out which one is used in your case to track the error further to an OS call.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • I think [this function](https://github.com/python/cpython/blob/3.6/Python/thread_pthread.h#L310) is used for my env, I tried to figure out how to check the log in `dprintf` here, it seems `dprintf` need two parameters, but there is only one parameter here, I'm really curious about this. – wangzhe Jan 13 '19 at 18:30
  • @wangzhe this means that `sem_init` failed but `errno` is `0` somehow. – ivan_pozdeev Jan 13 '19 at 18:43
  • I've no idea how that could happen. You need to debug this. – ivan_pozdeev Jan 13 '19 at 18:44
0

There is a function named sem_init in my program, which may conflict with the system library, the program runs ok after I modify the name of this function(but still not sure the reason).

wangzhe
  • 573
  • 1
  • 5
  • 13
  • C has no namespaces, so if you have a function with the same name as a library function, it can easily override it: whatever code wants to call the library function will call yours instead, leading to an undesired result. Actually, you have likely received (and blissfully ignored) a compiler warning about this. – ivan_pozdeev Jan 28 '19 at 05:31