Skip to content

Instantly share code, notes, and snippets.

@anoduck
Last active November 25, 2023 21:16
Show Gist options
  • Save anoduck/7f14620868a2ca47b50263d87302d859 to your computer and use it in GitHub Desktop.
Save anoduck/7f14620868a2ca47b50263d87302d859 to your computer and use it in GitHub Desktop.
Script repeating `trio.ClosedResourceError`
import trio
from aioresult import ResultCapture
import string
async def task(receiver):
new_list = []
list_nums = []
alpha_nums = []
async with receiver:
async for x in receiver:
if str(x).isdigit():
list_nums.append(x)
if str(x).isalpha():
alpha_nums.append(x)
for letter in alpha_nums:
for number in list_nums:
value = str(letter).join(number)
new_list.append(value)
await trio.sleep(0.1)
return new_list
async def mailer(i, sender):
async with sender:
await sender.send(i)
async def send_it(sender):
letters = string.ascii_letters
numbers = string.digits
abc_list = [[x for x in letters].extend([y for y in numbers])]
async with trio.open_nursery() as send_nurse:
[send_nurse.start_soon(mailer, i, sender.clone()) for i in abc_list]
async def main():
sender, receiver = trio.open_memory_channel(1)
async with trio.open_nursery() as nursery:
async with sender, receiver:
nursery.start_soon(send_it, sender)
return1 = ResultCapture.start_soon(nursery, task, receiver.clone())
result1 = return1.result()
print('{0} return type = {1}'.format(result1, type(result1)))
if __name__ == "__main__":
trio.run(main)
@anoduck
Copy link
Author

anoduck commented Nov 25, 2023

Running the script with 39 and 40 swapped, resulted in the execution of the script hanging.

async def main():
    sender, receiver = trio.open_memory_channel(1)
    async with sender, receiver:
        async with trio.open_nursery() as nursery:
            nursery.start_soon(send_it, sender)
            return1 = ResultCapture.start_soon(nursery, task, receiver)
    await trio.sleep(0.1)
    result1 = await return1.result()
    print('{0} return type = {1}'.format(result1, type(result1)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment