标签: python-trio

如何在python-trio中的KeyboardInterrupt之后清理连接

我的班级在连接到服务器时应立即发送登录字符串,然后在会话结束后应发送退出字符串并清理套接字。下面是我的代码。

import trio

class test:

    _buffer = 8192
    _max_retry = 4

    def __init__(self, host='127.0.0.1', port=12345, usr='user', pwd='secret'):
        self.host = str(host)
        self.port = int(port)
        self.usr = str(usr)
        self.pwd = str(pwd)
        self._nl = b'\r\n'
        self._attempt = 0
        self._queue = trio.Queue(30)
        self._connected = trio.Event()
        self._end_session = trio.Event()

    @property
    def connected(self):
        return self._connected.is_set()

    async def _sender(self, client_stream, nursery):
        print('## sender: started!')
        q = self._queue
        while True:
            cmd = await q.get()
            print('## sending to the server:\n{!r}\n'.format(cmd))
            if self._end_session.is_set():
                nursery.cancel_scope.shield = True …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-3.5 python-3.6 python-trio

3
推荐指数
1
解决办法
408
查看次数

从托儿所对象中捕获返回值

使用trio和nurseryobjects时,如何捕获从方法返回的任何值?

就拿从三人的网站例如:

async def append_fruits():
    fruits = []
    fruits.append("Apple")
    fruits.append("Orange")
    return fruits

async def numbers():
    numbers = []
    numbers.append(1)
    numbers.append(2)
    return numbers

async def parent():
    async with trio.open_nursery() as nursery:
        nursery.start_soon(append_fruits)
        nursery.start_soon(numbers)
Run Code Online (Sandbox Code Playgroud)

我修改它,以便每个方法返回一个list.你如何捕获返回值以便我可以打印它们?

python-3.x python-trio

3
推荐指数
1
解决办法
199
查看次数

如何使用父子函数收集结果和使用限制

我想要实现的是产生多个父母的东西,每个父母都做一些工作,然后产生一些孩子来检查其他事情并在父母中获取这些结果以做进一步的工作。我还试图制定 2 个不同的重生限制,因为父母的工作可以做的比孩子多。

我将如何做到这一点?

如果我不使用 limit2,它会起作用,但我想要两个限制器。

import trio
import asks
import time
import random

async def child(parent, i, sender, limit2):
    async with limit2:
        print('Parent {0}, Child {1}: started! Sleeping now...'.format(parent, i))
        #await trio.sleep(random.randrange(0, 3))
        print('Parent {0}, Child {1}: exiting!'.format(parent, i))
        async with sender:
            await sender.send('Parent {0}, Child {1}: exiting!'.format(parent, i))

async def parent(i, limit):
    async with limit:
        print('Parent {0}: started! Sleeping now...'.format(i))
        #await trio.sleep(random.randrange(0, 3))

        sender, receiver = trio.open_memory_channel(10)
        limit2 = trio.CapacityLimiter(2)
        async with trio.open_nursery() as nursery:
            for j in …
Run Code Online (Sandbox Code Playgroud)

python-3.x python-trio

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

我怎么能用三重奏异步读取文件的特定行

所以我想用三重奏(异步)打开文件,然后由于文件很大,读取它的单个特定行

所以在“普通”同步 python 中,我会做这样的事情:

with open("text.txt") as f:
    for i, line in enumerate(f):
        if i == 3:
            print(line)
Run Code Online (Sandbox Code Playgroud)

这将打印文件第二行的内容

现在的问题是,当使用 trio 的 open_file 方法时,enumerate(f)返回错误: TypeError: 'AsyncIOWrapper' object is not iterable

并遵循文档:

async with await trio.open_file("text.txt") as f:
    async for i, line in f:
        print(i)
        print(line)
Run Code Online (Sandbox Code Playgroud)

只会为 i 返回该行的值,而对于该行只返回空格

那么,如何使用三重奏/异步来读取大文件的特定行而不会丢失太多内存?

python asynchronous python-3.x python-trio

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