[Pyrex] module scope, and threads

ahalda at cs.mcgill.ca ahalda at cs.mcgill.ca
Sun Sep 3 03:04:10 UTC 2006


Hi again,

It turns out my pyrex still needed a tweak or two since last time.

Here is one new issue which I solved but it took me a few tries, just in
case anyone is interested (or is inexperienced like me). Also maybe there
are suggestions.

I was trying to modify a module-level variable from within a function, EG:

cdef int modVar

def myFunc():
   modVar = 3
   return modVar

but, similarly to my last problem, there is a local implicit 'cdef object
modVar' masking the module level variable, so that it doesn't get changed.
So it is not scoping the way it looks. The solution is to do 'global
modVar' at the start of the function. (it helped to know that implicit
thing)


This issue is actually part of a larger issue. This bigger issue is that I
have a c function which takes an object pointer as an argument, and
creates a thread in the backgruond to process the object (it plays some
music). So it returns immediately, but keeps on using the object. Then in
python, if I create such an object locally in a function, and call this c
function (through python) on the object, when the python function exits
the object is deleted from memory since python does not realize that there
is the c-thread still using it.

My solution was to create a module level pyrex object to keep at least one
reference to the object active, for as long as the background thread needs
the object.

Allan











More information about the Pyrex mailing list