Embedded Python : Why does thread lock here?

roschler robert.oschler at gmail.com
Tue Jul 7 14:01:17 EDT 2009


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.  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.  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?

The code with the relevant parts excerpted is shown below, hopefully
the indents hold up.

Thanks,
Robert

----------------------------

# MODULE: PythonThreadTest.py

# 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."

# Import needed system modules
import sys, os
from threading import Thread

# ----------------- BEGIN: THREAD class to execute robodanceCommand()
class threadRun(Thread):
    def __init__ (self, theJobID = None):
        Thread.__init__(self)
        self.jobCompleted = False
    # def: __init__
    def run(self):
        try:
        	# NOTE ---> This is where the thread locks if I don't call
join(0.001) in
        	#  my DELPHI (not Python) loop that waits on the thread to
complete.  Once
        	#  theNewThread.join() is called, execution resumes in
        	#  deleteOutputVariables() and the thread completes.
            deleteOutputVariables(Test_PYTHON, ["PyOut1"])

            # Let others know we are done.
            self.jobCompleted = True
        except Exception, exc:
            self.exceptionCaught = exc
            # Let others know we are done.
            self.jobCompleted = True
            print("(Python::threadRun) Exception occurred.")
        # end: try
    # def: run()
# ----------------- END: THREAD to execute robodanceCommand()

theNewThread = None
theNewThread = threadRun("TestThread")
theNewThread.start()



More information about the Python-list mailing list