Skyscanner / aiotask-context

Contextual information for asyncio tasks
MIT License
161 stars 25 forks source link

Catch AttributeError when context does not exist #12

Closed argaen closed 7 years ago

argaen commented 7 years ago

If the factory hasn't been set, we will see errors like

        if task:
>           return asyncio.Task.current_task().context.get(key, default)
E           AttributeError: '_asyncio.Task' object has no attribute 'context'

Instead of propagating this exception, an alternative way of dealing with this is:

logger.warning('task_factory for context module was not installed, returning default')
return default

In the set method, we would just show the warning.

This avoids impact when users are not aware of this module, forget to initialize the factory (or maybe they just don't want to), tests...

I'm not sure either this is something we want though, @pfreixes what do you think?

pfreixes commented 7 years ago

Umm, I had the same thought a few days ago. Finally, for consistency, I prefer to leave the clock-bomb on the user's hands. If they do not configure the factory they must configure it. Trying to make all happy - tests, not well configured environments, etc - might head us to weak environments.

shivagupta-byte commented 4 months ago

I'm facing a similar issue now, what's the fix for this? I'm new to aiotask and not sure which factory are we talking here to initialize. can someone help here

jsoucheiron commented 4 months ago

There's more info in the readme loop.set_task_factory(context.task_factory)