python多進程登錄遠(yuǎn)端服務(wù)器
發(fā)布日期:2021-12-20 10:28 | 文章來源:CSDN
通過Semaphore
來控制對共享資源的的訪問數(shù)量,可以控制同一時刻并發(fā)的進程數(shù) 。
#/usr/bin/python # _*_ coding: utf-8 _*_ import multiprocessing import time import paramiko def ssh(s,i,host):
try:
s.acquire() print(time.strftime('%H:%M:%S'),multiprocessing.current_process().name + " 獲得鎖運行"); ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=host, port=22, username="root", password="yankefei") print (host+" is login success") stdin, stdout, stderr = ssh.exec_command("echo d a t e && df -hl") print(stdout.read().decode('utf-8')) returncode = stdout.channel.recv_exit_status() print("returncode:",returncode)
except:
ssh.close() # time.sleep(i) print(time.strftime('%H:%M:%S'),multiprocessing.current_process().name + " 釋放鎖結(jié)束"); s.release() print (host+" is unreachable")
finally:
ssh.close() s.release() if __name__ == "__main__": s = multiprocessing.Semaphore(200) #同時并發(fā)200個進程 for n in range(111): p = multiprocessing.Process(target = ssh, args=(s,2,"192.168.0."+str(n))) p.start()
運行結(jié)果如下圖:
到此這篇關(guān)于python多進程登錄遠(yuǎn)端服務(wù)器的文章就介紹到這了,更多相關(guān)多進程 Python內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章