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

Fixed the issue of unable to reload using pycharm #2211

Closed
wants to merge 2 commits into from
Closed

Fixed the issue of unable to reload using pycharm #2211

wants to merge 2 commits into from

Conversation

liuyiyangwang
Copy link

Summary

Unfortunately, starting from uvicorn 0.21.1, when developing with PyCharm, reload cannot terminate the process properly.

The os.kill and signal.CTRL_C_EVENT used in the previous function are based on UNIX system signals, which may not work as expected on Windows. On Windows, the signal mechanism is significantly different from UNIX systems, which might be the reason why it cannot be reloaded normally.

    def restart(self) -> None:
        if sys.platform == "win32":  # pragma: py-not-win32
            self.is_restarting = True
            assert self.process.pid is not None
            os.kill(self.process.pid, signal.CTRL_C_EVENT)
        else:  # pragma: py-win32
            self.process.terminate()
        self.process.join()

        self.process = get_subprocess(
            config=self.config, target=self.target, sockets=self.sockets
        )
        self.process.start()

Many versions can't reload properly anymore, I have modified the restart function as:

    def restart(self) -> None:
        self.process.terminate()
        self.process.join()
        self.process = get_subprocess(
            config=self.config, target=self.target, sockets=self.sockets
        )
        self.process.start()

pycharm will appear it will hang for multiple minutes on the process.join() call

After modification The reload is pretty much instant, like it was on earlier versions(0.21.1).
@liuyiyangwang liuyiyangwang closed this by deleting the head repository Jan 21, 2024
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.

1 participant