66import getpass
77import stat
88import time
9+ import uvloop
910import logging
1011from io import BytesIO
1112from datetime import datetime
1415from threading import Thread
1516from fnmatch import fnmatchcase
1617log = logging .getLogger ('iris' )
18+ uvloop .install ()
1719
1820
1921TMP_PREFIX = '.iris-tmp.'
@@ -44,7 +46,7 @@ def run(tasks):
4446 tasks = [tasks ]
4547
4648 loop = asyncio .get_event_loop ()
47- res = loop .run_until_complete (gather_with_concurrency (128 , * tasks ))[0 ] # TODO: Gather concurrency?
49+ res = loop .run_until_complete (gather_with_concurrency (32 , * tasks ))[0 ] # TODO: Gather concurrency?
4850 return res
4951
5052
@@ -295,6 +297,7 @@ def __init__(self, path, host, dry=False, pattern='*', ignore_pattern='//', key=
295297 self ._sftp = None
296298 self ._last_check = 0
297299 self .open_sem = asyncio .Semaphore (128 ) # Max open files?
300+ self .open_sessions = asyncio .Semaphore (8 ) # Max open files?
298301 self .req = set ()
299302
300303 @property
@@ -471,16 +474,18 @@ async def _files_path(self, path=None):
471474 async def get_content (self , path , cb ):
472475 fd = BytesIO ()
473476 try :
474- size = (await self .sftp .lstat (path )).size
475- async with self .open_sem :
476- async with self .sftp .open (path , 'rb' , block_size = 65536 * 100 ) as src :
477- data = True
478- while data :
479- data = await src .read (size = self .CHUNK_SIZE )
480- if data :
481- fd .write (data )
482- if cb is not None and size > self .CHUNK_SIZE :
483- cb (self .CHUNK_SIZE / size )
477+ async with self .open_sessions :
478+ async with self .conn .start_sftp_client () as sftp :
479+ size = (await sftp .lstat (path )).size
480+ async with self .open_sem :
481+ async with sftp .open (path , 'rb' , block_size = 65536 ) as src :
482+ data = True
483+ while data :
484+ data = await src .read (size = self .CHUNK_SIZE )
485+ if data :
486+ fd .write (data )
487+ if cb is not None and size > self .CHUNK_SIZE :
488+ cb (self .CHUNK_SIZE / size )
484489 if cb is not None :
485490 cb (False )
486491 except asyncssh .SFTPNoSuchFile :
@@ -500,20 +505,23 @@ async def get_file(self, path):
500505 async def _writefile (self , origin , target , mtime , cb ):
501506 path = self .sftp .encode (os .path .dirname (target ))
502507
503- await self .sftp .makedirs (path , exist_ok = True )
504- if not await self .sftp .isdir (path ):
505- raise asyncssh .SFTPFailure (f'{ path } is not a directory' ) from None
506-
507- data = BytesIO (origin ).read ()
508- async with self .open_sem :
509- async with self .sftp .open (target , 'wb' , block_size = 65536 * 100 ) as dst :
510- ith = 0
511- while ith * self .CHUNK_SIZE < len (origin ):
512- await dst .write (data [ith * self .CHUNK_SIZE : (ith + 1 ) * self .CHUNK_SIZE ])
513- ith += 1
514- if cb is not None and len (origin ) > self .CHUNK_SIZE :
515- cb (self .CHUNK_SIZE / len (origin ))
516- await dst .utime (times = (mtime .timestamp (), mtime .timestamp ()))
508+ async with self .open_sessions :
509+ async with self .conn .start_sftp_client () as sftp :
510+
511+ await sftp .makedirs (path , exist_ok = True )
512+ if not await sftp .isdir (path ):
513+ raise asyncssh .SFTPFailure (f'{ path } is not a directory' ) from None
514+
515+ data = BytesIO (origin ).read ()
516+ async with self .open_sem :
517+ async with sftp .open (target , 'wb' , block_size = 65536 ) as dst :
518+ ith = 0
519+ while ith * self .CHUNK_SIZE < len (origin ):
520+ await dst .write (data [ith * self .CHUNK_SIZE : (ith + 1 ) * self .CHUNK_SIZE ])
521+ ith += 1
522+ if cb is not None and len (origin ) > self .CHUNK_SIZE :
523+ cb (self .CHUNK_SIZE / len (origin ))
524+ await dst .utime (times = (mtime .timestamp (), mtime .timestamp ()))
517525 if cb is not None :
518526 cb (True )
519527
0 commit comments