This commit is contained in:
yumoqing 2023-12-17 12:21:17 +08:00
parent 0feae81eb8
commit 24ea2c6bdc
2 changed files with 53 additions and 39 deletions

View File

@ -1,5 +1,6 @@
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
@ -18,6 +19,7 @@ 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 = []
@ -107,48 +109,54 @@ 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] keyin = True
xmsgs.reverse() a = xmsgs.pop(0)
xmsgs.pop() while True:
xmsgs.reverse() if a[1] is None:
proc.stdin.write_eof()
self.running = False
else:
s = a[1].format(**ns) s = a[1].format(**ns)
proc.stdin.write(s) proc.stdin.write(s)
if debug_input:
debug_input(f'{s=},{a=}') if len(xmsgs) == 0 or xmsgs[0][0]:
keyin = True 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()
while True: self.running = True
if proc.stdout.at_eof(): while self.running:
break
if keyin: if keyin:
keyin = False keyin = False
await proc.stdin.drain() await proc.stdin.drain()
if proc.stdout.at_eof():
if len(xmsgs) > 0 and already_output: break
f = partial(feed_data, xmsgs, show_input) tup = proc.collect_output()
callee = loop.call_later(xmsgs[0][0], f) x = tup[0]
x = await proc.stdout.read(1024) if x!='' and show_stdout:
if x is None:
break
if callee: if callee:
callee.cancel() callee.cancel()
if show_stdout: callee = None
show_stdout(x) show_stdout(x)
already_output = True 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##########')
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,21 +1,27 @@
import random import random
import asyncio import asyncio
import inspect
from functools import wraps from functools import wraps
def asyncCall(func): def to_func(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 Worker: class AsyncWorker:
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:
@ -27,16 +33,16 @@ class Worker:
return stdout, stderr return stdout, stderr
if __name__ == '__main__': if __name__ == '__main__':
async def hello(cnt,greeting): 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 = Worker() w = AsyncWorker()
tasks = [ w(hello,i,'hello world') for i in range(1000) ] g = [ asyncio.create_task(w(hello,i,'hello world')) for i in range(1000) ]
await asyncio.wait(tasks) await asyncio.wait(g)
print('aaaaaaaaaaaaaaaaaaa')
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(run()) loop.run_until_complete(run())