[python-uk] Extending Python - Asynchronous Callbacks

Phil Hornby phil.hornby at accutest.co.uk
Fri Aug 15 13:06:14 EDT 2003


Me and another guy have done some stuff with Asynchronos callbacks from
another thread...took a bit of playing to get it to work...We had several
instances of long uptimes but after, sometimes, 3 days you would have a
clash...the joys of concurrency...

Ended up with something like this, which seems to work:

    PyObject *resultobj ;
    long result ;

    PyEval_RestoreThread(this->pPythonCBThreadState); // pretend that we're
the callback python thread

    PyObject *pValue=NULL;

    #ifdef NDEBUG
    fprintf(stdout, "[Callback]: called\n");
    #endif

    // check that python object is callable
    if (this->pCallbackFunc && PyCallable_Check(this->pCallbackFunc)) {

        // Do callback to Python
        pValue = PyObject_CallObject(this->pCallbackFunc,pArgs);

        // check return
        if (pValue != NULL) {
            if (PyInt_Check(pValue)) {
                #ifdef NDEBUG
                fprintf(stdout, "[Callback]: call returned: %ld\n",
PyInt_AsLong(pValue));
                #endif
                result = NOTIF_STATUS_OK;
            }
            else if (PyFloat_Check(pValue)) {
                #ifdef NDEBUG
                fprintf(stdout, "[Callback]: call returned: %f\n",
PyFloat_AsDouble(pValue));
                #endif
                result = NOTIF_STATUS_OK;
            }
            else if (PyString_Check(pValue)) {
                #ifdef NDEBUG
                fprintf(stdout, "[Callback]: call returned: %s\n",
PyString_AsString(pValue));
                #endif
                result = NOTIF_STATUS_OK;
            }
            else {
                #ifdef NDEBUG
                fprintf(stderr, "[Callback][Warning]: invalid return type,
not int, float or string\n");
                #endif
                result = NOTIF_STATUS_WARN;
            }
            Py_DECREF(pValue);
        }

        else {
            // Python callback error
            #ifdef _DEBUG
            fprintf(stderr,"[Callback][Error]:");
            PyErr_Print();
            fprintf(stderr,"[Callback][Error]: call failed\n");
            #endif
            result = NOTIF_STATUS_ERR;
        } // if
    }

    else {
        // Callback function has been corrupted (or not properly
initialised)
        #ifdef NDEBUG
        PyErr_Print();
        fprintf(stderr, "[Callback][Error]: callback function invalid\n");
        #endif
        result = NOTIF_STATUS_ERR;
    } // if

    resultobj = PyInt_FromLong((long)result) ;

Wondered if anyone else has done anything like this...and if so if their
solution was similar? Or if they have a better way of doing it....

--
Phil Hornby
Accutest Ltd.
(01457) 891121
mailto::phil.hornby at accutest.co.uk

-----Original Message-----
From: python-uk-bounces at python.org
[mailto:python-uk-bounces at python.org]On Behalf Of Michael Hudson
Sent: 15 August 2003 11:37
To: python-uk at python.org
Subject: [python-uk] Re: Regional meetings


"Phil Hornby" <phil.hornby at accutest.co.uk> writes:

> Anyone else out there doing much in the way of Python
Extensions/Embedding?

Well, I've written a fair pile of C for the Python core, which is
fairly similar to extending Python... I steer clear of embedding,
though.  I also wrote the first version of

    http://www.python.org/doc/current/ext/defining-new-types.html

> As that is my main area of interest so wouldn't mind swapping notes with
> anyone who has done some...

What kind of notes?  Find a bit of the Python core that does more or
less what you want, copy it and change it so it does exactly what you
want :-) Well, there's a bit more to it than that, sometimes.

Cheers,
mwh

--
 All parts should go together without forcing. You must remember that
 the parts you are  reassembling were disassembled by you.  Therefore,
 if you can't get them together again, there must be a reason. By all
 means, do not use a hammer.           -- IBM maintenance manual, 1925


_______________________________________________
python-uk mailing list
python-uk at python.org
http://mail.python.org/mailman/listinfo/python-uk




More information about the python-uk mailing list