Skip to content

Instantly share code, notes, and snippets.

@hanksudo
Created January 23, 2024 07:25
Show Gist options
  • Save hanksudo/ce96040461390259e299f5aefe210ac9 to your computer and use it in GitHub Desktop.
Save hanksudo/ce96040461390259e299f5aefe210ac9 to your computer and use it in GitHub Desktop.
coroutine experiment in python and go
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")
}
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