Ejemplo n.º 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;
}
Ejemplo n.º 2
0
void stackless_loop(struct uwsgi_server *uwsgi) {

	int i;
	PyTaskletObject *int_tasklet;

	// tasklets main loop
	for(;;) {
		//uwsgi->async_running = -1 ;
		//if (PyStackless_GetRunCount() > 0) {
			uwsgi->async_running = 0 ;
		//}
		uwsgi->async_nevents = async_wait(uwsgi->async_queue, uwsgi->async_events, uwsgi->async, uwsgi->async_running, 0);

                if (uwsgi->async_nevents < 0) {
                        continue;
                }

		for(i=0; i<uwsgi->async_nevents;i++) {

                        if (uwsgi->async_events[i].ASYNC_FD == uwsgi->serverfd) {
				//pass the connection to the first available tasklet
				fprintf(stderr,"sending new connection...\n");
				PyChannel_Send(uwsgi->workers_channel, Py_True);
			}

		}

		/*
		if (PyStackless_GetRunCount() > 0) {
			PyStackless_Schedule(Py_None, 0);
		}
		*/

		PyStackless_RunWatchdogEx( 10, PY_WATCHDOG_TOTALTIMEOUT);

		//int_tasklet = (PyTaskletObject *) PyStackless_RunWatchdog( 1000 );
		/*
		fprintf(stderr,"done watchdog %p\n", int_tasklet);
		if (!PyTasklet_IsCurrent(int_tasklet)) {
			fprintf(stderr,"re-insert: %d\n", 1);// PyTasklet_Insert(int_tasklet));
		}
		fprintf(stderr,"recycle\n");
		*/
	}

}