Compare commits

..

No commits in common. "aeeaaa6e36747767881ff8eb0a489f13789da736" and "77851c9d146a3c0446aed0d978742be24882d84d" have entirely different histories.

2 changed files with 39 additions and 53 deletions

View File

@ -1,6 +1,5 @@
import os import os
import sys import sys
import time
import shlex import shlex
from functools import partial from functools import partial
from threading import Thread from threading import Thread
@ -19,7 +18,6 @@ class SSHNode:
"password":password, "password":password,
"port":port "port":port
} }
print(self.server2)
self.jumpers = jumpers self.jumpers = jumpers
self.conn = None self.conn = None
self.jumper_conns = [] self.jumper_conns = []
@ -111,54 +109,48 @@ class SSHNode:
term_size=(80,24), term_size=(80,24),
encoding='utf-8' encoding='utf-8'
) )
buffer = ''
msglen = len(xmsgs)
for i in range(msglen):
if xmsgs[i][0]:
break
s = xmsgs[i][1].format(**ns)
proc.stdin.write(s)
keyin = False keyin = False
def feed_data(xmsgs, debug_input): def feed_data(xmsgs, debug_input):
if len(xmsgs) == 0: if len(xmsgs) == 0:
print('#####++##### xmsgs has zero elements')
return return
a = xmsgs[0]
xmsgs.reverse()
xmsgs.pop()
xmsgs.reverse()
s = a[1].format(**ns)
proc.stdin.write(s)
if debug_input:
debug_input(f'{s=},{a=}')
keyin = True keyin = True
a = xmsgs.pop(0)
while True:
if a[1] is None:
proc.stdin.write_eof()
self.running = False
else:
s = a[1].format(**ns)
proc.stdin.write(s)
if len(xmsgs) == 0 or xmsgs[0][0]:
break
a = xmsgs.pop(0)
already_output = False already_output = False
callee = None callee = None
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
self.running = True while True:
while self.running: if proc.stdout.at_eof():
break
if keyin: if keyin:
keyin = False keyin = False
await proc.stdin.drain() await proc.stdin.drain()
if proc.stdout.at_eof():
break
tup = proc.collect_output()
x = tup[0]
if x!='' and show_stdout:
if x is None:
break
if callee:
callee.cancel()
callee = None
show_stdout(x)
else:
if callee is None:
if len(xmsgs) > 0:
f = partial(feed_data, xmsgs, show_input)
t = xmsgs[0][0] or 0
callee = loop.call_later(t, f)
await asyncio.sleep(0.05)
print('##########fininshed##########') if len(xmsgs) > 0 and already_output:
f = partial(feed_data, xmsgs, show_input)
callee = loop.call_later(xmsgs[0][0], f)
x = await proc.stdout.read(1024)
if callee:
callee.cancel()
if show_stdout:
show_stdout(x)
already_output = True
async def _run(self, cmd, input=None, stdin=None, stdout=None): async def _run(self, cmd, input=None, stdin=None, stdout=None):
if cmd.startswith('l2r'): if cmd.startswith('l2r'):

View File

@ -1,27 +1,21 @@
import random import random
import asyncio import asyncio
import inspect
from functools import wraps from functools import wraps
def to_func(func): def asyncCall(func):
@wraps(func) @wraps(func)
def wraped_func(*args,**kw): def wraped_func(*args,**kw):
if inspect.iscoroutinefunction(func): task = asyncio.ensure_future(func(*args,**kw))
task = asyncio.ensure_future(func(*args,**kw)) asyncio.gather(task)
ret = asyncio.gather(task)
return ret
return func(*args, **kw)
return wraped_func return wraped_func
class AsyncWorker: class Worker:
def __init__(self,maxtask=50): def __init__(self,maxtask=50):
self.semaphore = asyncio.Semaphore(maxtask) self.semaphore = asyncio.Semaphore(maxtask)
async def __call__(self,callee,*args,**kw): async def __call__(self,callee,*args,**kw):
async with self.semaphore: async with self.semaphore:
if inspect.iscoroutinefunction(callee): return await callee(*args,**kw)
return await callee(*args,**kw)
return callee(*args, **kw)
async def run(self,cmd): async def run(self,cmd):
async with self.semaphore: async with self.semaphore:
@ -33,16 +27,16 @@ class AsyncWorker:
return stdout, stderr return stdout, stderr
if __name__ == '__main__': if __name__ == '__main__':
def hello(cnt,greeting): async def hello(cnt,greeting):
t = random.randint(1,10) t = random.randint(1,10)
# await asyncio.sleep(t) await asyncio.sleep(t)
print(cnt,'cost ',t,'seconds to',greeting) print(cnt,'cost ',t,'seconds to',greeting)
async def run(): async def run():
w = AsyncWorker() w = Worker()
g = [ asyncio.create_task(w(hello,i,'hello world')) for i in range(1000) ] tasks = [ w(hello,i,'hello world') for i in range(1000) ]
await asyncio.wait(g) await asyncio.wait(tasks)
print('aaaaaaaaaaaaaaaaaaa')
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(run()) loop.run_until_complete(run())