Sep-06-2019, 02:02 AM
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!
the output is below:
the script hung after print "Exiting t1 Thu Aug 29 18:48:37 2019"
without writing "u2pyruncapture-67277-722095" to file
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
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)) |
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