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);
        }

}
Exemple #2
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;
}
Exemple #3
0
int
main(int argc, char **argv)
{
	PyObject *main, *game;
	PyObject *func, *args;
	PyTaskletObject *process;
	Py_SetProgramName(argv[0]);
	Py_Initialize();
	/* get the time python methods */
	timemod = PyImport_ImportModule("time");
	/* create a module for the game */
	game = Py_InitModule("game", game_methods);
	/* run the definition in main module */
	main = PyImport_AddModule("__main__");
	PyRun_SimpleString(python_prog); /* runs in main */
	/* start the python function as a tasklet */
	func = PyObject_GetAttrString(main, "everySecond");
	process = PyTasklet_New(NULL, func);
	Py_DECREF(func);
	/* initialize the tasklet args */
	args = PyTuple_New(0);
	PyTasklet_Setup(process, args, NULL);
	Py_DECREF(args);

	/* now let's run the "game engine" */
	run_game_engine();

	Py_Finalize();
	return 0;
}
static PyObject *
tasklet_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyFunctionObject *func = NULL;
    static char *kwlist[] = {"func", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tasklet",
                                     kwlist, &func))
        return NULL;
    return (PyObject*) PyTasklet_New(type, (PyObject*)func);
}
Exemple #5
0
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");
}