Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_default_locale() is broken #9

Open
espositofulvio opened this issue Sep 14, 2017 · 0 comments
Open

get_default_locale() is broken #9

espositofulvio opened this issue Sep 14, 2017 · 0 comments

Comments

@espositofulvio
Copy link

Using thread locals for coroutines is not what you want

import asyncio
from threading import local
locals = local()
async def coro1():
   locals.locale = 1
   print(locals.locale)
   await asyncio.sleep(2)
   print(locals.locale)

async def coro2():
   locals.locale = 2
   print(locals.locale)
asyncio.get_event_loop().run_until_complete(asyncio.wait([coro1(), coro2()]))

[output]
1
2
2

If coro1 is executed before coro2, after the await it'll see modifications done by coro2 as they share the same thread. In the same way, a call to get_default_locale() after an await might return a different locale if another coroutine has changed it in the meantime. It's better to have users rely only on request.locale or you need to implement something like thread locals but for tasks.

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

No branches or pull requests

1 participant