Created
January 23, 2024 07:25
-
-
Save hanksudo/ce96040461390259e299f5aefe210ac9 to your computer and use it in GitHub Desktop.
coroutine experiment in python and go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func a() string { | |
fmt.Println("b") | |
go func() { | |
time.Sleep(time.Second) | |
fmt.Println("a") | |
}() | |
return "return a" | |
} | |
func main() { | |
fmt.Println(a()) | |
time.Sleep(5 * time.Second) | |
fmt.Println("end") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def a(): | |
print("b") | |
async def async_inner(): | |
await asyncio.sleep(1) | |
print("a") | |
asyncio.create_task(async_inner()) | |
return "return a" | |
async def main(): | |
result = await a() | |
print(result) | |
await asyncio.sleep(5) | |
print("end") | |
# Run the asynchronous main function | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment