私は読み取りが含まれているとasyncio
接続のストリームを書き込むために使用されConnection
オブジェクトがあります。サーバー側でストリームから取得する正しい方法は何ですか?
class Connection(object):
def __init__(self, stream_in, stream_out):
object.__init__(self)
self.__in = stream_in
self.__out = stream_out
def read(self, n_bytes : int = -1):
return self.__in.read(n_bytes)
def write(self, bytes_ : bytes):
self.__out.write(bytes_)
yield from self.__out.drain()
、connected
はConnection
オブジェクトにクライアントが接続するたびに作成されますが、その後、4つのバイトを読み込みます。
@asyncio.coroutine
def new_conection(stream_in, stream_out):
conn = Connection(stream_in, stream_out)
data = yield from conn.read(4)
print(data)
そして、クライアント側では、4バイトが書き出されます。
@asyncio.coroutine
def client(loop):
...
conn = Connection(stream_in, stream_out)
yield from conn.write(b'test')
は、これはほとんど期待が、私はyield from
すべてのread
とwrite
コールに持っている作品として。私は複数の場所からread
とwrite
を呼び出す場合、私がいないことを好むだろう
def read(self, n_bytes : int = -1):
data = yield from self.__in.read(n_bytes)
return data
むしろデータを取得するよりも、私は
<generator object StreamReader.read at 0x1109983b8>
のような出力が得られます。私はConnection
内部からINGのyield from
を試してみました毎回yield from
を繰り返します。むしろそれらをConnection
の内部に保ちます。私の究極の目標はこれに私のnew_conection
機能を削減します
@asyncio.coroutine
def new_conection(stream_in, stream_out):
conn = Connection(stream_in, stream_out)
print(conn.read(4))
なぜあなたは降伏する必要がありますか? conn.read(4)から返さなければ、バイトオブジェクトを返すように見えます。それはあなたがここで探しているものですか? – RageCage
@RageCage:収率from'ing 'なし' conn.read(4) '依然として発生返し:私は明らかにしておく必要があり、<0x1019262b0でジェネレータオブジェクトConnection.readを>' ' –
申し訳ありません。 conn.read()の最初の反復(1行バージョン)から結果が得られない場合、結果は何ですか? – RageCage