Skip to content

Conversation

@mgmacias95
Copy link

Having a script with two independent event loops, only one of them is displayed in the table. This PR fixes that.

import asyncio
import threading

async def factorial(name, number):
    f = 1
    for i in range(2, number + 1):
        print(f"Task {name}: Compute factorial({number}), currently i={i}...")
        await asyncio.sleep(1)
        f *= i
    print(f"Task {name}: factorial({number}) = {f}")
    return f

async def main():
    # Schedule three calls *concurrently*:
    L = await asyncio.gather(
        factorial("A", 200),
        factorial("B", 300),
        factorial("C", 400),
    )
    print(L)

async def main2():
    await asyncio.sleep(100)


t1 = threading.Thread(target=asyncio.run, args=(main(),))
t2 = threading.Thread(target=asyncio.run, args=(main2(),))

t1.start()
t2.start()
t1.join()
t2.join()

@pablogsal pablogsal merged commit d92b520 into pablogsal:GH-91048-tasks May 3, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants