Skip to content

Instantly share code, notes, and snippets.

@genadyp
Last active February 3, 2026 07:02
Show Gist options
  • Select an option

  • Save genadyp/5b14f140ca9b74336ebbb987b883e5f2 to your computer and use it in GitHub Desktop.

Select an option

Save genadyp/5b14f140ca9b74336ebbb987b883e5f2 to your computer and use it in GitHub Desktop.
python useful links
@genadyp
Copy link
Author

genadyp commented Sep 12, 2023

How to launch independent subprocess in Python

https://stackoverflow.com/a/42499675

#!/usr/bin/env python
import os
import sys
import platform
from subprocess import Popen, PIPE

# set system/version dependent "start_new_session" analogs
kwargs = {}
if platform.system() == 'Windows':
    # from msdn [1]
    CREATE_NEW_PROCESS_GROUP = 0x00000200  # note: could get it from subprocess
    DETACHED_PROCESS = 0x00000008          # 0x8 | 0x200 == 0x208
    kwargs.update(creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, close_fds=True)  
elif sys.version_info < (3, 2):  # assume posix
    kwargs.update(preexec_fn=os.setsid)
else:  # Python 3.2+ and Unix
    kwargs.update(start_new_session=True)

p = Popen(["C"], stdin=PIPE, stdout=PIPE, stderr=PIPE, **kwargs)
assert not p.poll()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment