int main(int argc, char **argv) { PyObject *gameModule, *utilModule, *func, *ret, *args; PyTaskletObject *tasklet; int i; Py_SetProgramName(argv[0]); Py_Initialize(); niceChannel = PyChannel_New(NULL); utilModule = Py_InitModule("util", util_methods); gameModule = PyImport_ImportModule("game"); if (gameModule == NULL) { CheckForErrors(); } /* Allow the startup script "game.py" to start some tasklets in its Run method. I initially tried calling it directly as a function but it seemed to be called twice. */ func = PyObject_GetAttrString(gameModule, "Run"); if (func == NULL) { CheckForErrors(); } tasklet = PyTasklet_New(NULL, func); Py_DECREF(func); PyTasklet_SetBlockTrap(tasklet, 1); args = PyTuple_New(0); PyTasklet_Setup(tasklet, args, NULL); Py_DECREF(args); PyTasklet_Run(tasklet); while (1) { for (i = niceChannel->balance; i < 0; i++) { PyChannel_Send(niceChannel, Py_None); CheckForErrors(); } do { ret = PyStackless_RunWatchdog(2000000); if (ret != NULL && ret != Py_None) { PyTasklet_Kill((PyTaskletObject *)ret); CheckForErrors(); } Py_XDECREF(ret); } while (!ret); CheckForErrors(); } Py_DECREF(utilModule); Py_DECREF(gameModule); Py_DECREF(niceChannel); Py_Finalize(); return 0; }
static PyObject * channel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { static char *argnames[] = {NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, ":channel", argnames)) return NULL; return (PyObject *)PyChannel_New(type); }
void stackless_init(struct uwsgi_server *uwsgi) { int i; struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ; PyObject *tasklet_worker = PyCFunction_New(uwsgi_stackless_worker, NULL); uwsgi->workers_channel = PyChannel_New(NULL); uwsgi->stackless_table = malloc( sizeof(struct stackless_req*) * uwsgi->async); if (!uwsgi->stackless_table) { uwsgi_error("malloc()"); exit(1); } for(i=0;i<uwsgi->async;i++) { uwsgi->stackless_table[i] = malloc(sizeof(struct stackless_req)); if (!uwsgi->stackless_table[i]) { uwsgi_error("malloc()"); exit(1); } memset(uwsgi->stackless_table[i], 0, sizeof(struct stackless_req)); } fprintf(stderr,"initializing %d tasklet...", uwsgi->async); // creating uwsgi->async tasklets for(i=0;i<uwsgi->async;i++) { wsgi_req->tasklet = PyTasklet_New(NULL, tasklet_worker); uwsgi->stackless_table[i]->tasklet = wsgi_req->tasklet; uwsgi->stackless_table[i]->wsgi_req = wsgi_req; // useless for now, it will be used for I/O or other messaging uwsgi->stackless_table[i]->channel = NULL; wsgi_req->async_id = i ; PyTasklet_Setup(wsgi_req->tasklet, PyTuple_New(0), NULL); //PyTasklet_Run(wsgi_req->tasklet); wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ; } fprintf(stderr,"done\n"); }
static PyObject * channel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { return (PyObject *)PyChannel_New(type); }