Beispiel #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;
}
Beispiel #2
0
static PyObject *
run_watchdog(PyObject *self, PyObject *args, PyObject *kwds)
{
	static char *argnames[] = {"timeout", NULL};
	long timeout = 0;

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:run_watchdog",
					 argnames, &timeout))
		return NULL;
	return PyStackless_RunWatchdog(timeout);
}