2

I'm researching the asyncio library and I cannot understand completely the reason behind event loop policies.

So far it's clear that this interface is a factory that returns event loop for current context. The definition of the context is not clear though. Apparently, the interface of asyncio.AbstractEventLoopPolicy assumes that concrete implementation will be able to extract context on its own because neither method accepts arguments. This model works well for the event-loop-per-thread default policy, but usage under more sophisticated circumstances are under the question.

Looks like context isn't an arbitrary entity, but should follow certain rules (e.g. being visible globally). I wonder if such rules are listed anywhere. Are there examples of custom policies around?

1
  • 1
    aiogevent and gblub both have custom EventLoopPolicys. It seems to be most commonly used in libraries that integrate asyncio with other event loops. Commented May 2, 2015 at 14:52

1 Answer 1

2

Custom policy is required when you implement non-default event loop.

The main problem is: I want to create new event loop by asyncio.new_event_loop(). It's required for threads: only main thread has implicit event loop, all other threads should instantiate loop explicitly.

But I don't know specific event loop type, thus I need a factory for loop creation -- pluggable event loop policy.

Sign up to request clarification or add additional context in comments.

2 Comments

What does make "factory" a policy? This word implies some set of rules :)
IIRC Guido van Rossum called it personally. Policy is common pattern: see en.wikipedia.org/wiki/Policy-based_design

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.