----------------------------- sz ~/.ssh/ 拷贝到本地 crt管理 -------------------------ssh import paramiko private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='10.0.0.7', port=52113, username='song', pkey=private_key) # 执行命令 stdin, stdout, stderr = ssh.exec_command('df') # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close() ---------线程和进程-------------------- 线程就是计算机能够操作的最小单位 线程就是一堆指令,被包含在进程内。 @:内存:1s/12g CPU:2.7G 2.7亿指令 进程就是一个程序各种资源的集合 进程要操作cpu必须要创建一个进程,不会执行,依赖于线程。 线程和进程一样快 线程创建比较快 同一个进程里的线程可以互相通信 进程间通信 必须通过一个中间的代理 主线程就是程序本身 看不到 ----------------------------------- cpu 单核 上下文的切换 感觉是在多任务 python 无论多少核 同一时间只有一个线程 just run time gil lock ----------------- import threading,time def run(n): print("task",n) time.sleep(2) print("task done") start_time=time.time() t_objs=[] #very cool for i in range(50): t=threading.Thread(target=run,args=("t-%s"%i,)) # t2=threading.Thread(target=run,args=("t2",)) t.setDaemon(True) #把当前线程设置为守护进程 t.start() t_objs.append(t) #t2.start() # for t in t_objs: daemon # t.join() #主线程 print("all done",threading.current_thread(),threading.active_count()) print("cost:",time.time()-start_time) #么有等其他线程 多线程 并行 ----------------------gil---lock------------------ import threading,time def run(n): lock.acquire() #加锁后程序变串行 global num num+=1 lock.release() lock=threading.Lock() num=0 t_objs=[] #very cool for i in range(50): t=threading.Thread(target=run,args=("t-%s"%i,)) t.start() t_objs.append(t) # for t in t_objs: # t.join() 等待子线程完毕 #主线程 print("all done",threading.current_thread(),threading.active_count()) print("num:",num) ------------------------------------- Rlock 递归锁 ------------------------ 信号量 semaphore www.cnblogs.com/alex3714/articles/5230609.html ======= import time import threading event = threading.Event() def lighter(): count=0 event.set() #绿灯 while True: if count>5 and count < 10: #改为红灯 event.clear() #清除标志类 print("\033[41;1mred light is on...\033[0m") elif count >10: event.set() #变成绿 count=0 else: print("\033[42;1mgreen light is on...\033[0m") time.sleep(1) count+=1 def car(name): while True: if event.is_set(): print("[%s] running....."%name) else: print("[%s] sees red light....." %name) event.wait() print("\033[34;1m[%s] green is start going.....\033[0m" % name) light=threading.Thread(target=lighter,) light.start() car1=threading.Thread(target=car,args=("testla",)) car1.start() --------------------------队列------------- import queue q=queue.Queue(maxsize=3) #设置put 大小 q.put("s1") q.put("s2") q.put("s3") q.qsize() q.get() #数据取完会卡死 q.get(timeout=1) q.get_nowait() -------------------- q=queue.Queue(maxsize=3) last in first out queue.PriorityQueue 存储时可以设置优先级 q.put((-1,"fsdf")) first out q.put((3,"fsdf")) q.put((10,"fsdf")) q.put((6,"fsdf")) -----------------生产者消费者模型--------------------- import queue import threading,time q=queue.Queue(10) def Produce(name): count=1 while True: q.put("骨头%s"%count) print("生产了骨头",count) count+=1 time.sleep(2) def Consumer(name): #while q.qsize()>0: while True: print("[%s]取到[%s]并且吃到了他。。。"%(name,q.get())) time.sleep(0.5) p=threading.Thread(target=Produce,args=("alex",)) m= threading.Thread(target=Consumer, args=("chenonghau",)) w= threading.Thread(target=Consumer, args=("wangsen",)) p.start() m.start() w.start()