Created
September 28, 2022 11:37
-
-
Save serxoz/7c4495ab3e55b94f4a2f96f25d870ce6 to your computer and use it in GitHub Desktop.
Exemplo de fork en python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import sys | |
import os | |
def fai_cousas(): | |
# Fai cousas durante 10 segundos e logo sae | |
time.sleep(10) | |
print("Feito") | |
sys.exit(0) | |
if __name__ == "__main__": | |
print("Hola! Este é un exemplo de como facer un fork(). Vai crear " | |
"un proceso fillo que se executará de fondo e logo sairá.\n" | |
"Ós 10 segundos debería aparecer un 'Feito'...") | |
try: | |
pid = os.fork() | |
if pid > 0: | |
# Saíndo do proceso pai | |
sys.exit(0) | |
except OSError as e: | |
print("fallou o fork: %d (%s)" % (e.errno, e.strerror)) | |
sys.exit(1) | |
# Configuración para o entorno do proceso fillo | |
os.chdir("/") | |
os.setsid() | |
os.umask(0) | |
# Executa de fondo | |
fai_cousas() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment