0

So I have two python programs:

main.py sub.py 

Main contains import sub.py, and sub.py contains os._exit(0). Problem is that I don't want to close both programs, only sub.py. Is this possible?

2
  • os._exit(0) terminates the main process, so...dont use that...or fork the process in main.py before launching sub.py Commented Jul 2, 2018 at 9:59
  • 1
    You are starting one process as python main.py and if you import sub.py it becomes part of that process. It is not a program on its own. Commented Jul 2, 2018 at 10:02

2 Answers 2

1

A simple answer: use return instead os._exit(0).

Usually using os._exit(0) is a bad idea, if you have to - do it from __main__ in sub.py only. If you don't have other option, you can start sub.py as a new process from main.py with python subprocess module.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't have "two python programs", you have two python modules, one of them used as the "main" script (the one your actually execute) and importing the other. At runtime you only have one process, and os._exit() kills this process, period.

The fix is very obviously to not use os._exit() at all.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.