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

comment #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

comment #3

wants to merge 2 commits into from

Conversation

richardjlyon
Copy link
Collaborator

@richardjlyon richardjlyon commented Mar 25, 2022

The idea I'm exploring is replacing the RouteDispatcher class with a simple Subscribe/Post Event pattern between the Route handler, Transaction processor, and database. Attempting to implement an EventManager has raised a few issues:

  1. Although the motivation for doing this was to reduce code coupling (through the RouteDispatcher class), it's actually caused a circular import. The function to register events imports the EventManager instance created in app.py, which is breaking things.

  2. I had a crude version working previously that used simple functions and a dictionary in a module (events.py). I could watch the subscribers dictionary filling up during app initialisation, but when I came to post_events after starting uivicorn, the dictionary was empty. So something I'm assuming is global isn't. Why is that, and how best to structure this? I created a class so an instance could hold the state, but passing this around caused the circular import.

  3. There are a few global objects floating around - the database, the route dispatcher (when I was using it), this event manager - where do these live?

  4. Stupid one. A tutorial online advised resolving circular imports by importing a whole module (event_manager.EventManager() ) rather than from it (from event_manager import EventManager()). I realised I've no idea what's happening - I couldn't refer to event_manager.EventManager().

  5. in server/routes/accounts.py I can call post_event when the api is called. But how do I get the results back to return to FastAPI? How does the thing posting an event get the results of all the listeners doing their thing? Presumably it also has to subscribe to an event that is posted after everything is done?

@arlyon
Copy link
Contributor

arlyon commented Mar 27, 2022

Uvicorn launches multiple workers. You can specify how many you want with the --workers flag i think. The effect of this is that global state won't work. This is probably a good thing as you'll want to design your solution to scale horizontally (ie more copies).

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

Successfully merging this pull request may close these issues.

2 participants