1. 程式人生 > >Python線程包裝器

Python線程包裝器

rgs target true std pro roc sta __main__ art

import threading
import subprocess

import time


def need_thread(func, *args, **kwargs):
    def fun():
        print "sub:" + str(threading.current_thread().ident)
        time.sleep(1)
        sub = func(*args, **kwargs)
        print sub.stdout.read()
        print "sub down"

    def inner():
        t 
= threading.Thread(target=fun) t.start() return inner if __name__ == __main__: need_thread(subprocess.Popen, "ls -al", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)() print "main:" + str(threading.current_thread().ident) print "main done"

Python線程包裝器