Hey pants, I'm creating a monrepo for multiple fas...
# general
f
Hey pants, I'm creating a monrepo for multiple fastapi servers. I've figured most things out, but right now I'm stuck at creating a development server. I intend to use the pex binaries for deployment using docker, but during development we can't constantly rebuild the containers. I use uvicorn for serving, there are 2 ways to start a uvicorn server, from the shell
uvicorn module:app --reload
and programmatically
uvicorn.run(app)
Unfortunately uvicorn discontinued usage of reload while programmatically running the server, so for reload feature to work(crucial in a development environment), the command has to be executed via shell. I can't for the life of me figure out how to run a shell command or create a shell entrypoint with pants. Also it would be great if you guys could provide point me to an actual real world project with pants for a reference to make sure I'm doing this correctly.
πŸ‘‹ 2
h
@witty-crayon-22786 will
--loop
work in this case?
w
it won’t currently, but it could be made to: https://github.com/pantsbuild/pants/issues/9462#issuecomment-716821870
πŸ™Œ 1
h
Hi @full-keyboard-92367! So the issue here is that Pants runs each process in a "sandbox", to ensure consistency and avoid unpredictable side-effects. But what that means is that if you run uvicorn via Pants (say via a
pex_binary
target) it will run from the sandboxed copy of your code, not the original in your source tree. So it won't "see" edits in your source tree.
One solution is to run uvicorn outside of Pants (in a virtualenv that Pants can build for you)
The other is to use Pants's own file-watching capabilities via the
--loop
flag.
But that would require addressing the issue Stu linked to above, which I can try and take a look at in the next day or two.
We've been wanting to fix this wart for a while, so maybe now is the time
The nice thing about
--loop
(as opposed to relying on uvicorn's own file watching) is that it will do the right thing even in complicated situations, such as changes to .proto files.
f
One solution is to run uvicorn outside of Pants (in a virtualenv that Pants can build for you)
Ya I tried that, I followed https://www.pantsbuild.org/docs/python-third-party-dependencies#tip-set-up-a-virtual-environment-optional to create a venv, but I require pant's dependency injection to import a "common" module into my servers.
h
Is this common module in your source tree?
f
3rdparty src project_1 project_2 common this is my directory structure
h
I see, and are
project_1
,
project_2
and
common
different source roots (https://www.pantsbuild.org/docs/source-roots) ?
Is that the issue?
f
Yes
h
So you can try adding them all to PYTHONPATH
For example, internally we have a script called
python
that contains:
Copy code
PYTHONPATH=src/python:${PYTHONPATH:-} /path/to/venv/bin/python $@
(simplified from what we actually do internally, to avoid confusion)
Of course this is all a bit clunky, but should hold you over until we get a chance to implement the ticket above
m
Hmm, so there's no way to use loop for this? (until https://github.com/pantsbuild/pants/issues/9462 is closed)
w
Not really... You could use
--loop
with
package
and relaunch when a new file is produced maybe?
But we really ought to resolve that one soon: sorry for the delay.
m
don't apologize πŸ˜… thanks for the project btw! 😎 πŸ‘
❀️ 2
but right now, how ppl are dealing with this? If I have a docker-compose, I need to restart every time?
I'm preparing a setup with skaffold, so I've created a custom build command (which takes 20-30 sec to reload - it's acceptable considering the size/restart of pods), and I didn't notice this πŸ˜…
w
… thanks for the reminder. i’ll bump https://github.com/pantsbuild/pants/issues/9462 up my list.