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

Restart workers when code changes in development mode #31

Closed
2 of 4 tasks
Tracked by #53
rszamszur opened this issue Feb 1, 2022 · 6 comments · Fixed by #35
Closed
2 of 4 tasks
Tracked by #53

Restart workers when code changes in development mode #31

rszamszur opened this issue Feb 1, 2022 · 6 comments · Fixed by #35
Assignees
Labels
copier: project Related to copier template enhancement New feature or request
Milestone

Comments

@rszamszur
Copy link
Member

rszamszur commented Feb 1, 2022

It would be useful to have reloading changes in development mode. Either as a new option --reload to template CLI serve command, or automatically based on env variable, ex: FASTAPI_ENV=development FASTAPI_ENV=production, similar to Ruby on Rails. Not yet sure which approach would be better. Moreover, just adding --reload flag to WSGI + ASGI combo doesn't work properly, at least from initial tests. It's possible that for development in order to have reloading changes app would need to run on pure uvicorn (ASGI).

TODO:

  • Research if it's possible to have working reloading changes running gunicorn with uvicorn workers
  • Decide on the way in which this feature should be implemented.
  • Implement feature + tests
  • Update template README
@rszamszur rszamszur added enhancement New feature or request copier: project Related to copier template labels Feb 1, 2022
@jeanlst
Copy link

jeanlst commented Feb 1, 2022

From my experience it's kind of a pain to use reloading with the WSGI + ASGI combo, so for development I usually go with pure uvicorn and just mount the volume. It's worked fine so far

@rszamszur
Copy link
Member Author

@jeanlst I agree. This approach would be much easier to implement and less buggy.

I'm having difficulties deciding on the way in which this feature should be implemented.
Currently, the generated project has serve command for running "production" gunicorn + uvicorn combo.

For now, I have thought of:

  1. Implementing a separate command. (but feels a bit redundant).
  • serve for running development mode.
  • production for running WSGI + ASGI.
  1. Automatically based on env variable, ex: FASTAPI_ENV=development FASTAPI_ENV=production, similar to Ruby on Rails. However, this could result in mutually excluding options (differences in gunicorn vs uvicorn cli options). Moreover, it would require careful planning. For instance, where in the future this would be used and how? And as much as I like Rails I found this approach very confusing, especially for beginners. You are developing an app that after switching to production doesn't work properly because you need to set up other things you didn't know of (puma, precompile assets, etc.). It can have advantages, ex: database, application configuration. But I guess the same can be achieved with pydantic BaseSettings and .env support.
  2. Implement development mode to fastapi-mvc instead of template. So that all the development tools are built into fastapi-mvc package, and generated project is a production-ready FastAPI application. In the future, there can be generators for models, migrations, controllers, etc similar to rails generate ..., and it makes sense that such tools are being built in the package, not the template.

I personally lean towards solution number 3.

@abnerjacobsen
Copy link

@rszamszur I worked a little more with fastapi-mvc and put on github another example of using it with logs via Loguru, working with Gunicorn/Uvicorn, without any log problem with log rotation in Gunicorn with multiple Workers. I didn't change anything in wsgi.py and used --config to specify the Gunicorn configuration file specific to Loguru.

For development I use Uvicorn and run it via this configuration in pyproject.toml:

mvc-demo-dev = 'mvc_demo.wsgi_uvicorn:run_dev_wsgi'

The repository is this:

https://github.com/abnerjacobsen/fastapi-mvc-loguru-demo

His readme explains what I did.

I agree with @jeanlst , hot reloading with Gunicorn is almost impossible, not worth spending time on.

And I also agree with your idea of ​​implementing a development mode to fastapi-mvc and, thus, instead of it being a template generator from the cookiecutter, it becomes a deployment generator/tester for environments like Kubernetes, docker-compose or serverless (I personally use the AWS Lambda solution a lot).

@rszamszur
Copy link
Member Author

@abnerjacobsen, @jeanlst, thanks for the input, I really appreciate it.

At the moment I'm working on adding the documentation and development server. After that, I'll jump into the topic of logging with loguru.

@jeanlst
Copy link

jeanlst commented Feb 4, 2022

@abnerjacobsen that looks great, thanks for the repo using loguru! I'm a bit on the boat of using the standard logging lib instead of loguru tho. Maybe I'll stop being stubborn and use loguru but that's unlikely

@rszamszur rszamszur added this to the 0.8.0 milestone Feb 6, 2022
@rszamszur rszamszur self-assigned this Feb 6, 2022
@rszamszur rszamszur linked a pull request Feb 6, 2022 that will close this issue
7 tasks
@rszamszur
Copy link
Member Author

@abnerjacobsen I've used your piece of code for running uvicorn programmatically, works like charm!

@rszamszur rszamszur mentioned this issue Feb 15, 2022
58 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
copier: project Related to copier template enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants