Skip to content

Instantly share code, notes, and snippets.

@wusuopu
Created April 22, 2013 12:40
Show Gist options
  • Save wusuopu/5434609 to your computer and use it in GitHub Desktop.
Save wusuopu/5434609 to your computer and use it in GitHub Desktop.
在Python api中使用线程
#include <Python.h>
#include <pythread.h>
static void test(void *s)
{
while (1)
{
printf("in sub thread\n");
sleep(1);
}
}
static PyObject *
thread_test(PyObject *self)
{
printf("in thread_test: %ld\n", PyThread_start_new_thread(test, NULL));
Py_RETURN_TRUE;
}
static PyMethodDef lc_thread_test_methods[] = {
{"test", (PyCFunction)thread_test, METH_NOARGS, ""},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initlc_thread_test(void)
{
Py_InitModule("lc_thread_test", lc_thread_test_methods);
PyEval_InitThreads();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment