Common API¶
-
class
aioftp.
StreamIO
(reader, writer, *, timeout=None, read_timeout=None, write_timeout=None)¶ Stream input/output wrapper with timeout.
Parameters: - reader (
asyncio.StreamReader
) – stream reader - writer (
asyncio.StreamWriter
) – stream writer - timeout (
int
,float
orNone
) – socket timeout for read/write operations - read_timeout (
int
,float
orNone
) – socket timeout for read operations, overrides timeout - write_timeout (
int
,float
orNone
) – socket timeout for write operations, overrides timeout
-
close
()¶ Close connection.
-
read
(count=-1)¶ asyncio.coroutine()
Proxy for
asyncio.StreamReader.read()
.Parameters: count ( int
) – block size for read operation
-
readexactly
(count)¶ asyncio.coroutine()
Proxy for
asyncio.StreamReader.readexactly()
.Parameters: count ( int
) – block size for read operation
-
readline
()¶ asyncio.coroutine()
Proxy for
asyncio.StreamReader.readline()
.
-
write
(data)¶ asyncio.coroutine()
Combination of
asyncio.StreamWriter.write()
andasyncio.StreamWriter.drain()
.Parameters: data ( bytes
) – data to write
- reader (
-
class
aioftp.
Throttle
(*, limit=None, reset_rate=10)¶ Throttle for streams.
Parameters: -
append
(data, start)¶ Count data for throttle
Parameters:
-
clone
()¶ Clone throttle without memory
-
limit
¶ Throttle limit
-
wait
()¶ asyncio.coroutine()
Wait until can do IO
-
-
class
aioftp.
StreamThrottle
¶ Stream throttle with read and write
aioftp.Throttle
Parameters: - read (
aioftp.Throttle
) – stream read throttle - write (
aioftp.Throttle
) – stream write throttle
-
clone
()¶ Clone throttles without memory
-
classmethod
from_limits
(read_speed_limit=None, write_speed_limit=None)¶ Simple wrapper for creation
aioftp.StreamThrottle
Parameters:
- read (
-
class
aioftp.
ThrottleStreamIO
(*args, throttles={}, **kwargs)¶ Bases:
aioftp.common.StreamIO
Throttled
aioftp.StreamIO
. ThrottleStreamIO is subclass ofaioftp.StreamIO
. throttles attribute is dictionary of name:aioftp.StreamThrottle
pairsParameters: - *args –
positional arguments for
aioftp.StreamIO
- **kwargs –
keyword arguments for
aioftp.StreamIO
- throttles (
dict
withaioftp.Throttle
values) – dictionary of throttles
>>> self.stream = ThrottleStreamIO( ... reader, ... writer, ... throttles={ ... "main": StreamThrottle( ... read=Throttle(...), ... write=Throttle(...) ... ) ... }, ... timeout=timeout ... )
-
append
(name, data, start)¶ Update timeout for all throttles
Parameters:
-
iter_by_block
(count=8192)¶ Read/iterate stream by block.
Return type: aioftp.AsyncStreamIterator
>>> async for block in stream.iter_by_block(block_size): ... ...
-
iter_by_line
()¶ Read/iterate stream by line.
Return type: aioftp.AsyncStreamIterator
>>> async for line in stream.iter_by_line(): ... ...
-
read
(count=-1)¶ asyncio.coroutine()
aioftp.StreamIO.read()
proxy
-
readline
()¶ asyncio.coroutine()
-
wait
(name)¶ asyncio.coroutine()
Wait for all throttles
Parameters: name ( str
) – name of throttle to acquire (“read” or “write”)
-
write
(data)¶ asyncio.coroutine()
aioftp.StreamIO.write()
proxy
- *args –
-
class
aioftp.
AsyncListerMixin
¶ Add ability to async for context to collect data to list via await.
>>> class Context(AsyncListerMixin): ... ... >>> results = await Context(...)
-
class
aioftp.
AbstractAsyncLister
(*, timeout=None)¶ Abstract context with ability to collect all iterables into
list
via await with optional timeout (viaaioftp.with_timeout()
)Parameters: timeout ( None
,int
orfloat
) – timeout for __anext__ operation>>> class Lister(AbstractAsyncLister): ... ... @with_timeout ... async def __anext__(self): ... ...
>>> async for block in Lister(...): ... ...
>>> result = await Lister(...) >>> result [block, block, block, ...]
-
aioftp.
with_timeout
(name)¶ Method decorator, wraps method with
asyncio.wait_for()
. timeout argument takes from name decorator argument or “timeout”.Parameters: name ( str
) – name of timeout attributeRaises: asyncio.TimeoutError – if coroutine does not finished in timeout Wait for self.timeout
>>> def __init__(self, ...): ... ... self.timeout = 1 ... ... @with_timeout ... async def foo(self, ...): ... ... pass
Wait for custom timeout
>>> def __init__(self, ...): ... ... self.foo_timeout = 1 ... ... @with_timeout("foo_timeout") ... async def foo(self, ...): ... ... pass
-
aioftp.
async_enterable
(f)¶ Decorator. Bring coroutine result up, so it can be used as async context
>>> async def foo(): ... ... ... ... return AsyncContextInstance(...) ... ... ctx = await foo() ... async with ctx: ... ... # do
>>> @async_enterable ... async def foo(): ... ... ... ... return AsyncContextInstance(...) ... ... async with foo() as ctx: ... ... # do ... ... ctx = await foo() ... async with ctx: ... ... # do
-
aioftp.
setlocale
(name)¶ Context manager with threading lock for set locale on enter, and set it back to original state on exit.
>>> with setlocale("C"): ... ...