0
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
test_list = [1, 2, 3, 4]

for number in test_list:
    pool.apply_async(func=test_quit, args=[number, ])

pool.close()
pool.join()

when i call os._exit(), the program terminate successfully

def test_quit(number):
    if number == 1:
        print("1 should exit")
        os._exit()

but when i use os._exit(1), the program is still working.

def test_quit(number):
    if number == 1:
        print("1 should exit")
        os._exit(1)

so whats the difference of this two func, why os._exit(1) can not stop the program

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
dapaoming
  • 11
  • 1
  • Which operating system? On UNIXy platforms, `os._exit()` is _not_ a successful exit (but as an `os._anything()` function, its behavior is allowed to be locally different). – Charles Duffy Jul 18 '22 at 11:40
  • @Legit007, `os.exit(0)` would behave the same as `os.exit(1)` for the OP's purpose here; it's only `os.exit()`, with the argument missing that's different. – Charles Duffy Jul 18 '22 at 11:41

1 Answers1

1

os._exit() throws a TypeError because it's not a valid use (_exit's argument is mandatory), and thereby engages multiprocessing's error-handling machinery.

os._exit(1) stops the specific process you're in, but because you're using multiprocessing, your program consists of multiple processes, so stopping the one process does not stop the whole program. (This is one of the ways in which multiprocessing is different from multithreading in practice).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • so if i want to end the main processing when some error happen in sub processing, which func should i use. – dapaoming Jul 18 '22 at 11:48
  • None of the above. `os._exit()` in a multiprocessing-created process risks corrupting the communication channels out of your processes back to the main one. If "how do I abort on the first error in `multiprocessing`?" is the question you really meant to ask, I'd argue that it's already asked and answered as [Python multiprocessing: abort map on first child error](https://stackoverflow.com/questions/52272325/python-multiprocessing-abort-map-on-first-child-error). – Charles Duffy Jul 18 '22 at 11:49
  • (note that the above question's answer instructs the OP to use `apply_async()`, as you're already doing). – Charles Duffy Jul 18 '22 at 11:56