문제

I have a python script that has a raw input command, but I would like to run it in the background after the user inputs the raw_input part. The problem I have is if I try running the script in the background using &, the raw input pops up as a linux command and the python script doesn't recognize it.

Any tips?

도움이 되었습니까?

해결책

You can use fork to create a child process and then exit the parent.

#!/usr/bin/env python

import os
import sys
import time

_ = raw_input('Enter the the secret code: ')
if os.fork(): # returns 0 in the child, pid of the child in the parent
    sys.exit()

time.sleep(2)
print('All good things must come to an end')

다른 팁

You probably want to run the script in the foreground, but then call os.fork() after the user has input the value.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top