0
我正在嘗試編寫腳本從網絡設備使用Pexpect異步期望(python 3.5.1和pexpect從github)收集一些信息,並得到一些奇怪的事情:所有工作正常幾個設備,並不能與更多的工作(通常> 5-6)。我寫了這個簡單的腳本來進行測試:Pexpect:異步期望與幾個連接
@asyncio.coroutine
def test_ssh_expect_async(num):
print('Task #{0} start'.format(num))
p = pexpect.spawn('ssh localhost', encoding='utf8')
#p.logfile = sys.stdout
yield from p.expect('password', async=True)
p.sendline('***')
yield from p.expect(r'@self-VirtualBox\:', async=True)
p.sendline('uptime')
yield from p.expect(r'@self-VirtualBox\:', async=True)
p.sendline('uname -a')
yield from p.expect(r'@self-VirtualBox\:', async=True)
p.sendline('ll')
yield from p.expect(r'@self-VirtualBox\:', async=True)
print('Task #{0} end'.format(num))
@asyncio.coroutine
def test_loop():
tasks = []
for i in range(1, 5):
tasks.append(test_ssh_expect_async(i))
yield from asyncio.wait(tasks)
print('All Tasks done')
print('--------------Async--------------------')
loop = asyncio.get_event_loop()
loop.run_until_complete(test_loop())
如果我嘗試使用範圍(1,3)爲例子,我得到這樣的:
[email protected]:/media/sf_netdev$ python3 simple-test.py
--------------Async--------------------
Task #3 running
Task #1 running
Task #2 running
Task #3 closed
Task #1 closed
Task #2 closed
All Tasks done
但是,如果我增加上限,我得到了一些錯誤:
[email protected]:/media/sf_netdev$ python3 simple-test.py
--------------Async--------------------
Task #3 running
Task #1 running
Task #4 running
Task #2 running
Exception in callback BaseSelectorEventLoop.add_reader(11, <bound method...d=11 polling>>)
handle: <Handle BaseSelectorEventLoop.add_reader(11, <bound method...d=11 polling>>)>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/selector_events.py", line 234, in add_reader
key = self._selector.get_key(fd)
File "/usr/lib/python3.5/selectors.py", line 191, in get_key
raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '11 is not registered'
During handling of the above exception, another exception occurred:
...
爲什麼會發生?如何用異步pexpect編寫工作腳本?
---------------回答------------
這是一個錯誤https://github.com/pexpect/pexpect/issues/347。現在pexpect命令修復了它。
請編輯這個答案的基本內容,然後接受它作爲你的問題的答案。 –