我正在处理一段多线程代码,我想知道为什么Connection
没有__next__
与recv
混叠。这会让他们更善于处理。
您可以自己快速完成。
class MyConnection:
def __init__(self, connection):
self.connection = connection
def __iter__(self):
return self
def __next__(self):
return self.connection.recv()
def __getattr__(self, attr):
"""Expose connection APIs."""
return getattr(self.connection, attr)
def __dir__(self):
"""Forward introspection requests.
Enable autocompletion with IPython.
"""
return dir(self.__class__) + dir(self.connection)
r, w = multiprocessin.Pipe()
reader = MyConnection(r)
...
for data in reader:
print(data)