#! /usr/bin/python import subprocess import threading import time import random import logging myLogger = logging.getLogger('My') sleeptime = 0 global test_cycle test_cycle = 200 global result result = 0 def execCmd(command): infile = subprocess.PIPE outfile = subprocess.PIPE err = subprocess.PIPE p = subprocess.Popen(command, stdin=infile, stdout=outfile, stderr=err, close_fds=True) #p = subprocess.Popen(command, stdin=subprocess.PIPE) (out, err) = p.communicate() myLogger.info("cmd is %s, out is %s, err is %s.", command, out, err) class StorageThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): global result thread_c = 0 while thread_c < test_cycle: thread_c += 1 myLogger.info("call pipe for %d times.", thread_c) execCmd("cat") ran = random.random() #time.sleep(ran) print "@@@@@@@thread all excuted!" result = 1 class StatsThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): myLogger.info("simple thread started") #main formatstr = "%(threadName)s::%(levelname)s::%(asctime)s::%(module)s::%(lineno)d::%(name)s::(%(funcName)s) %(message)s" logging.basicConfig(format=formatstr, level=logging.DEBUG) handler = logging.StreamHandler() logging.addLevelName(5, 'TRACE') logging.TRACE = 5 #logging.root.handlers.append(logging.StreamHandler()) #handler = logging.FileHandler("/var/log/vdsm/testlog.txt") #logging.root.handlers.append(handler) storage_thread = StorageThread() storage_thread.start() thread_m = 0 while thread_m < test_cycle: new_thread = StatsThread() new_thread.start() #new_thread.join() ran = random.random() #time.sleep(ran) thread_m += 1 print "waiting for thread calling pipe complete" while result == 0: time.sleep(0.2) print "succeed!" print "sleeping" #time.sleep(1)