Example #1
0
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 void stackless_schedule_to_req() {

	int id = uwsgi.wsgi_req->async_id;
	uint8_t modifier1 = uwsgi.wsgi_req->uh->modifier1;

	// ensure gil
	UWSGI_GET_GIL

	if (!uwsgi.wsgi_req->suspended) {
		usl.sl[id] = PyTasklet_New(NULL, usl.callable);
		PyObject *args = PyTuple_New(0);
		PyTasklet_Setup(usl.sl[id], args, NULL);
		Py_DECREF(args);
		uwsgi.wsgi_req->suspended = 1;
	}

	// call it in the main core
        if (uwsgi.p[modifier1]->suspend) {
                uwsgi.p[modifier1]->suspend(NULL);
        }

	PyTasklet_Run(usl.sl[id]);

	// call it in the main core
        if (uwsgi.p[modifier1]->resume) {
                uwsgi.p[modifier1]->resume(NULL);
        }

}