سؤال

I have a question about correct process control under UNIX. I have FreeBSD 8.0 server and python application. I need start my python application process 3 times (client.py script). And if trying to start 4-th process by cron, he must do sys.exit().

Can you suggest me a CORRECT way control my processes? ps -auxww or pid file with number of threads. Maybe in Python libraries exists a ready solution?

Thanks!

هل كانت مفيدة؟

المحلول

In FreeBSD, ps shows the command line in its output. So for a Python schript, this is the name of the interpreter followed by the name of the script. Both can be full paths or just program names.

So you can something like this:

import subprocess
import sys

scriptname  = 'foo'
procs = subprocess.check_output(['ps', '-xa']).splitlines()
count = len([l for l in procs if 'python' in l and scriptname in l])
if count > 3:
    sys.exit()

نصائح أخرى

Although this does not answer the part of your question referring to cron, hence it does not resolve your problem, I think that the proper way to handle your processes would be with the use of subprocesses, either by using python's multiprocessing module, or by using python's fork() and waitpid() implementations (of course you can use threads instead, but they won't take advantage of multiple cores and CPU's under some platforms, yet, so they may not be of your interest).

I understand that this will not fit your specific problem's description, but you could have it in mind and design your future programs accordingly.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top