Python Forum
Python thread script gets defunct state
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python thread script gets defunct state
#1
I have 2functions

runPA(), it will call a product function to run 10seconds and generate a temp file in /tmp then automatically delete the temp file after 10seconds

getCapFileName(), it searches /tmp to find temp file name which generated by runPA(),write its name to a file

How can i launch above 2 functions at the same time in python?

tried using threading when running the code in linux it goes to defunct state eventually

it's hard to debug it in linux it's RHEL7.5,python3.7
appreciate any ideas, thanks!

def test_UNV28366(self):
    import re
    import threading
    import pdb
    import time

    pid=os.getpid()
    event =threading.Event()                
    forBreak=False
    self.uvorud()

    def getCapFileName():
        gPid=os.getpid() 
        sleep(1)
        fcFile=""
        U2TEMP="C:\\U2\\UV\\XDEMO"
        for root, dirs, files in os.walk(u2py.U2TEMP):
            for name in files:
                if re.match(r'u2pyruncapture\-\d+-\d+$', name):
                    sname=name.split("-")
                    #pdb.set_trace()
                    if int(sname[1]) == pid:   
                        #pdb.set_trace()                 
                        fcFile=name
                        print(fcFile)
                        #'''
            break

        return fcFile

    def runPA():
        rPid=os.getpid()
        #sleep(0.8)
        #if sysStr == 'Linux':        
        if TESTDB == 'uv':
            fVoc = u2py.File("VOC")                         
            dMpa = u2py.DynArray("")
            dMpa.insert(1, "PA")
            dMpa.insert(2, "DISPLAY before sleep")
            dMpa.insert(3, "SLEEP 10")
            dMpa.insert(4, "DISPLAY after sleep")
            fVoc.write("MPA", dMpa)
            fVoc.close()               

            cPa = u2py.Command("MPA")
            cPa.run(capture=True)  

    class MyThread(threading.Thread):
        def __init__(self, func, name='',daemon=None):
            threading.Thread.__init__(self)
            self.name = name
            self.func = func
            #self.result = self.func(*self.args)
            self.result = self.func()

        def run(self):
            print("\n","run at ",self.name,time.ctime())
            #self.func()
            if self.name=="t1":
                runPA()
            if self.name=="t2":
                getCapFileName()                    
            #print_time(self.name, 5, self.counter)
            #pdb.set_trace()
            print("Exiting ",self.name,time.ctime())    

        def get_result(self):
            try:
                return self.result
            except Exception:
                return None        
        '''
    #pdb.set_trace()
        '''
        #'''

    t1=MyThread(runPA,"t1")
    t2=MyThread(getCapFileName,"t2")
    t1.start()
    t2.start()        
    t1.join()
    t2.join()
    try:
        cFile=t2.get_result()
        print("cFile: ",cFile)
    except ValueError:
        print("cFile is None!")              

    self.replaceStdStreams()
    pdb.set_trace()
    self.repstdout.write(cFile)    
    self.repstdout.write("\n")
    self.restoreStdStreams()        

    assert(self.search_in_testoutput(cFile))
the output is below:
Quote:#python3 -m unittest -v u2py_command_test.TestCommandClass.test_UNV28366
test_UNV28366 (u2py_command_test.TestCommandClass) ...

run at t1 Thu Aug 29 18:48:27 2019

run at t2 Thu Aug 29 18:48:27 2019

u2pyruncapture-67277-722095

Exiting t2 Thu Aug 29 18:48:37 2019

Exiting t1 Thu Aug 29 18:48:37 2019

the script hung after print "Exiting t1 Thu Aug 29 18:48:37 2019"

without writing "u2pyruncapture-67277-722095" to file
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,004 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,788 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,247 May-28-2020, 05:27 PM
Last Post: micseydel
  Is there a way to not terminate a running thread in python Contra_Boy 3 1,932 May-05-2020, 09:38 PM
Last Post: SheeppOSU
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,385 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,583 May-11-2019, 08:12 PM
Last Post: keames
  State graph Kaluss 1 2,197 Mar-18-2019, 05:29 PM
Last Post: nilamo
  Python Thread stops execution neethuvp 1 3,354 Feb-18-2019, 06:36 PM
Last Post: micseydel
  Debugging inside a thread with Python 2.3.4 zerajera 0 1,675 Nov-21-2018, 01:43 PM
Last Post: zerajera
  How to run python script which has dependent python script in another folder? PrateekG 1 3,105 May-23-2018, 04:50 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020