/
tutorial-4.c
70 lines (52 loc) · 1.42 KB
/
tutorial-4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <Python.h>
#include <stdio.h>
#include <pthread.h>
int stop_event = 0;
void* run_python_function(void* arg)
{
PyGILState_STATE state = PyGILState_Ensure();
PyObject *pModule = NULL, *pFunc = NULL;
do
{
pModule = PyImport_ImportModule("work");
if (pModule == NULL) break;
pFunc = PyObject_GetAttrString(pModule, "working");
if (pFunc == NULL) break;
char* name = *((char**) arg);
while (!stop_event)
{
PyObject_CallFunction(pFunc, "s", name);
}
} while (0);
Py_XDECREF(pFunc);
Py_XDECREF(pModule);
PyGILState_Release(state);
pthread_exit(NULL);
return NULL;
}
int main(int argc, char* argv[])
{
PyEval_InitThreads();
Py_Initialize();
PyObject* sysPath = PySys_GetObject((char*) "path");
PyList_Append(sysPath, PyString_FromString("."));
PyThreadState* save = PyEval_SaveThread();
pthread_t tid1, tid2;
char* tname1 = "worker1";
char* tname2 = "worker2";
pthread_create(&tid1, NULL, &run_python_function, &tname1);
pthread_create(&tid2, NULL, &run_python_function, &tname2);
for (int i = 0; i < 5; i++)
{
printf("main thread is running\n");
sleep(1);
}
stop_event = 1;
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("finish\n");
PyEval_RestoreThread(save);
Py_Finalize();
pthread_exit(NULL);
return 0;
}