0
我有一個使用ssh定期登錄某個linux服務器並且服務器的最大連接數是13的腳本,所以我設置參數max_workers
10.但是我在後臺調度作業時發現,由於超過了ssh最大連接數,因此無法連接到Linux服務器。我不知道爲什麼會發生這種情況,因爲我限制了max_worker
。代碼簡化如下:APScheduler作業仍然運行一次,當它超過max_worker編號時添加到調度程序
import paramiko
from apscheduler.schedulers.background import BackgroundScheduler
from paramiko import SSHClient
class SSH(object):
def __init__(self):
self.scheduler = BackgroundScheduler({'apscheduler.executors.default': {'class': 'apscheduler.executors.pool:ThreadPoolExecutor', 'max_workers': '10'}})
def func(self, src='127.0.0.1', user='user', password='password'):
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(src, 22, user, password, timeout=3)
except Exception as e:
print 'Failed to connect to %s' % src
sys.exit(1)
def schedule(self, arg):
for i in range(100):
self.scheduler.add_job(self.func, 'interval',
seconds=60),
misfire_grace_time=100000000000,
kwargs=dict(
src=arg['source'],
user=arg['username'],
password=arg['password'],
))
try:
if not self.scheduler.running:
self.scheduler.start()
except Exception as e:
print e
if __name__ == '__main__':
ssh_client = SSH()
ssh_client.schedule(arg)