如何迭代连接

时间:2018-01-08 06:42:13

标签: python multiprocessing

我正在处理一段多线程代码,我想知道为什么function add (item, position){ var length = suits.length; for(i = length -1; i >= position; i--){ suits[length] = suits[i]; length--; }; suits[position] = item; }; add("Brooks Brothers",2) //to add it to the middle 没有Connection使用__next__别名。它会让他们处理得更好。

1 个答案:

答案 0 :(得分:1)

你可以自己快速完成。

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)