En Tue, 07 Jul 2009 15:01:17 -0300, roschler <robert.osch...@gmail.com> escribió:

I have the Python Intepreter embedded in a Delphi (Object Pascal)
program.  In the Python script shown below, I have a module that
creates a thread object and starts it.

Do you *execute* the module or do you *import* it?
Isn't a good idea to spawn a thread by side effect of importing a module.

The thread's run() call calls
a function called deleteOutputVariables() declared at the module
level.  In my code's first incarnation the thread's run() call would
deadlock when trying to call the deleteOutputVariables() function
declared at Module level.  I would never see the statement
"(deleteOutputVariables) Top of Call" printed to the screen.  I now
know that I must periodically call join() with a very fast time-out to
keep Python threads happy, and that solved the problem.

What does the Delphi code? Isn't a join with a long timeout enough?

However I am
curious as to why it deadlocked at the deleteOutputVariables() call?
Is it because deleteOutputVariables() is declared at the module level
or because that function deletes module level variables?  If so why?

I don't know, but you can make some experiments - move te deleteOutputVariables as a method, or don't delete module level variables at all, and see what happens. I'd say both factors are irrelevant.

# FUNCTION: Delete the variables given in the list.
def deleteOutputVariables(theModule, theOutputVariablesListOfNames):
    try:
        print "(deleteOutputVariables) Top of call."
        for theOutputVariableName in theOutputVariablesListOfNames:
            if theModule.__dict__.has_key(theOutputVariableName):
                print "(Python::deleteOutputVariables) Deleting the
Output Variable named " + theOutputVariableName
                del theModule.__dict__[theOutputVariableName]
    except:
        print "(deleteOutputVariables) Exception occurred."

As a rule, avoid using __special__ names. There is almost never need of using them (__init__ is a notable exception) unless you want to change Python behaviour.
In this case:

for theOutputVariableName in theOutputVariablesListOfNames:
  if hasattr(theModule, theOutputVariableName):
    delattr(theModule, theOutputVariableName)

(a name like theOutputVariablesListOfNames is too long for my taste, but that's a matter of style...)

theNewThread = None
theNewThread = threadRun("TestThread")

That first line is useless...

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to