Compare commits
2 Commits
77851c9d14
...
aeeaaa6e36
Author | SHA1 | Date | |
---|---|---|---|
|
aeeaaa6e36 | ||
|
24ea2c6bdc |
@ -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 = []
|
||||||
@ -109,48 +111,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]
|
|
||||||
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()
|
||||||
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 callee:
|
if x is None:
|
||||||
callee.cancel()
|
break
|
||||||
if show_stdout:
|
if callee:
|
||||||
|
callee.cancel()
|
||||||
|
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'):
|
||||||
|
@ -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):
|
||||||
task = asyncio.ensure_future(func(*args,**kw))
|
if inspect.iscoroutinefunction(func):
|
||||||
asyncio.gather(task)
|
task = asyncio.ensure_future(func(*args,**kw))
|
||||||
|
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:
|
||||||
return await callee(*args,**kw)
|
if inspect.iscoroutinefunction(callee):
|
||||||
|
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())
|
||||||
|
Loading…
Reference in New Issue
Block a user