小编And*_*rew的帖子

asyncio的性能

我正在努力熟悉asyncio,所以我决定编写一个数据库客户端.但是,性能与同步代码完全匹配.我确信这是我对一个概念的误解.有人可以解释我在做什么吗?

请参阅以下代码示例:

class Connection:
    def __init__(self, reader, writer, loop):
        self.futures = deque()

        # ...

        self.reader_task = asyncio.async(self.recv_data(), loop=self.loop)

    @asyncio.coroutine
    def recv_data(self):
        while 1:
            try:
                response = yield from self.reader.readexactly(4)
                size, = struct.unpack('I', response)
                response = yield from self.reader.readexactly(size)

                # ...                

                future = self.futures.popleft()

                if not future.cancelled():
                    future.set_result(response)

            except Exception:
                break

    def send_data(self, data):
        future = asyncio.Future(loop=self.loop)
        self.futures.append(future)

        self.writer.write(data)

        return future


loop = asyncio.get_event_loop()


@asyncio.coroutine
def benchmark():
    connection = yield from create_connection(loop=loop, ...)

    for i in range(10000):
        yield from connection.send_data(...)


s …
Run Code Online (Sandbox Code Playgroud)

python python-asyncio

10
推荐指数
1
解决办法
3374
查看次数

如何从&self中制作&mut?

我想暴露一个具有不可变self的公共函数,该函数调用具有可变self的私有函数.

struct Foo {
    value: i32,
}

impl Foo {
    fn f1(&self) {
        self.f2(); // <--- is it possible to make self mutable?
    }

    fn f2(&mut self) {
        self.value = 5;
    }
}

fn main() {
    let foo = Foo { value: 0 };
    foo.f1();
}
Run Code Online (Sandbox Code Playgroud)

编译此代码时出现错误

不可借用不可变借的内容*self是可变的

是否有可能让自己变得可变?

rust

2
推荐指数
2
解决办法
465
查看次数

标签 统计

python ×1

python-asyncio ×1

rust ×1