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

multiprocessing.Process.terminate not working with textual-serve #16

Open
kostrykin opened this issue Aug 23, 2024 · 0 comments
Open

multiprocessing.Process.terminate not working with textual-serve #16

kostrykin opened this issue Aug 23, 2024 · 0 comments

Comments

@kostrykin
Copy link

kostrykin commented Aug 23, 2024

I had already observed interferences between Textual and multiprocessing.Process before, those occurred when using threaded workers (see here), so maybe there is a deeper rooted issue with multiprocessing.Process in Textual?

This here is about a different issue with multiprocessing.Process. No threads are involved!

Consider the following MWE:

import contextlib
import multiprocessing
import pathlib
import time
import sys

from textual.app import App
from textual_serve.server import Server


def run_process():
    while True:
        time.sleep(1)


class ExampleApp(App):

    def on_mount(self):
        with open('log', 'w') as file:

            def log(text):
                file.write(f'{text}\n')
                file.flush()

            p = multiprocessing.Process(target = run_process)
            log('-- 1 --')
            p.start()
            log('-- 2 --')
            p.terminate()
            log('-- 3 --')
            p.join()
            log('-- 4 --')


if __name__ == "__main__":

    if '--web' in sys.argv:
        filename = pathlib.Path(__file__).stem
        server = Server(f'python -m {filename}')
        server.serve()

    else:
        app = ExampleApp()
        app.run()

Lets say the above is stored in mwe.py.

Running this without textual-serve, like python mwe.py or python -m mwe, yields the following output:

-- 1 --
-- 2 --
-- 3 --
-- 4 --

However, running the same example with textual-serve, like python test.py --web, yields a different output:

-- 1 --
-- 2 --
-- 3 --

So obviously, when running the MWE with textual-serve, the execution gets stuck on p.join(), meaning that p.terminate() didn't work properly. The obvious workaround is to add p.kill() after the p.terminate() line, however, the behavior of the code shouldn't diverge based on whether textual-serve is being used or not, right?


$ python --version
Python 3.11.0
$ pip list|grep -i textual
textual                0.76.0
textual-dev            1.5.1
textual-serve          1.0.3
@kostrykin kostrykin changed the title multiprocessing.Process.terminate not working properly with textual-serve multiprocessing.Process.terminate not working with textual-serve Aug 23, 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

No branches or pull requests

1 participant