Alt işlem için preexec_fn parametresini kullanın.Popen ve kaynak modülü. Örnek:
parent.py:
#!/usr/bin/env python
import os
import sys
import resource
import subprocess
def setlimits():
# Set maximum CPU time to 1 second in child process, after fork() but before exec()
print "Setting resource limit in child (pid %d)" % os.getpid()
resource.setrlimit(resource.RLIMIT_CPU, (1, 1))
print "CPU limit of parent (pid %d)" % os.getpid(), resource.getrlimit(resource.RLIMIT_CPU)
p = subprocess.Popen(["./child.py"], preexec_fn=setlimits)
print "CPU limit of parent (pid %d) after startup of child" % os.getpid(), resource.getrlimit(resource.RLIMIT_CPU)
p.wait()
print "CPU limit of parent (pid %d) after child finished executing" % os.getpid(), resource.getrlimit(resource.RLIMIT_CPU)
child.py:
#!/usr/bin/env python
import os
import sys
import resource
print "CPU limit of child (pid %d)" % os.getpid(), resource.getrlimit(resource.RLIMIT_CPU)
parent.py yeni bir işlem haline bölünür. Yeni işlemde, setlimits() öğesini arayacak, ardından child.py. Bu, kaynağın çocuk işleminde sınırlandırılacağı, ancak ebeveynte sınırlı olmayacağı anlamına gelir.
Çıktı çalışan programı:
./parent.py
CPU limit of parent (pid 17404) (-1, -1)
Setting resource limit in child (pid 17405)
CPU limit of parent (pid 17404) after startup of child (-1, -1)
CPU limit of child (pid 17405) (1, 1)
CPU limit of parent (pid 17404) after child finished executing (-1, -1)
buna genellikle çirkin parametreyi neden, özellikle de her zaman kabuk aracılığıyla alt işlemi yumurtlamaya için iyi bir fikir değil, bu durum birçok durumda ulimit kullanmaya çalışırken daha iyi bir çözümdür beladan alıntı yapmak.
Cevabım yerine en çok oylanan yanıtı kabul etmek isteyebilirsiniz. Benimkinden çok daha iyi. –