1、输入数据截取指定字段
1 mandatory_fields = ["-p","-s"] 2 for i in mandatory_fields: 3 if i not in self.args: 4 sys.exit("\033[31;1mLack of argument [%s]\033[0m" % i) 5 try: 6 self.ftp_host = self.args[self.args.index("-s") + 1] 7 self.ftp_port = int(self.args[self.args.index("-p") + 1]) 8 except (IndexError,ValueError): 9 self.help_msg() 10 sys.exit()
2.1、创建一个线程
1 #!/uer/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import threading 5 6 class MyThread(threading.Thread): 7 8 def __init__(self,name): 9 super(MyThread,self).__init__() 10 self.name = name 11 12 def run(self): 13 print("thread is running",self.name) 14 t = MyThread("jim") 15 t1 = MyThread("ALEX") 16 t.start() #开启一个线程 17 t1.start() #再开启一个线程
输出结果:
thread is running jim
thread is running ALEX
2.2、第二种方法
1 #!/uer/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import threading 5 def pao(num): 6 print("thread is runing",num) 7 8 t = threading.Thread(target=pao,args=(3,)) 9 t1 = threading.Thread(target=pao,args=(4,)) 10 t.start() 11 t1.start()
输出结果:
thread is runing 3
thread is runing 4
线程的阻塞:
-
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import threading 5 import time 6 7 def saihi(n): 8 print("hello %s"% n) 9 time.sleep(2) 10 11 if __name__=="__main__": 12 t_list=[] 13 for i in range(10): 14 n = threading.Thread(target=saihi ,args=[i,]) 15 n.start() 16 t_list.append(n) 17 for t in t_list: 18 t.join() #阻塞所有的线程执行完毕之后,再打印"==main==" 19 20 print("==main==")
守护线程:
当守护线程结束后,被守护的线程也同时结束!
m = threading.Thread(target=my_main,args=[] )
m.start()
m.setDaemon() #设置为守护线程