Python or OS forking/threading problem?
Florian Weimer
fw at deneb.cygnus.argh.org
Sat Mar 25 17:05:30 EST 2000
"Neil Schemenauer" <nascheme at enme.ucalgary.ca> writes:
> Not for my problem on Linux. My code didn't call sleep(). It
> may be a bug with the pthreads in libc6 for Linux. I can't
> reproduce it with C code though.
I think I can explain what happens. Look at the following code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
void *
thread(void * v)
{
for (;;) {
char buf[40];
int len = sprintf(buf, "%lu\n", (unsigned long) getpid());
write(1, buf, len);
sleep(1);
}
}
main()
{
pthread_t t;
pthread_create (&t, NULL, &thread, NULL);
fork();
sleep(3600);
}
This clearly shows that the thread is not duplicated on fork().
Now look at the Python source code:
static PyObject *
posix_fork(self, args)
PyObject *self;
PyObject *args;
{
int pid;
if (!PyArg_ParseTuple(args, ":fork"))
return NULL;
pid = fork();
if (pid == -1)
return posix_error();
PyOS_AfterFork();
return PyInt_FromLong((long)pid);
}
void
PyOS_AfterFork()
{
#ifdef WITH_THREAD
main_thread = PyThread_get_thread_ident();
main_pid = getpid();
#endif
}
long PyThread_get_thread_ident _P0()
{
volatile pthread_t threadid;
if (!initialized)
PyThread_init_thread();
/* Jump through some hoops for Alpha OSF/1 */
threadid = pthread_self();
return (long) *(long *) &threadid;
}
In the child process, all threads have disappeared, but the code doesn't
seem to be prepared to handle this.
More information about the Python-list
mailing list