1

Is it possible for a python script to execute at a low run level?

Edit: To clarify, is it possible for a python script to run in the background, kind of like a daemon.

Karim
  • 18,347
  • 13
  • 61
  • 70
pub
  • 21
  • 1
  • 2
    as Le Chiffre said in Casino Royale: Why not? – miku Jan 03 '10 at 14:23
  • 2
    "low runlevel"? You mean using `nice`? As in `nice python myscript.py`? Is that what you're asking? Please clarify your question. – S.Lott Jan 03 '10 at 14:24
  • `man nice` --> "nice -- execute a utility with an altered scheduling priority" – S.Lott Jan 03 '10 at 14:27
  • 1
    I believe he is talking about the sysv init runlevels. I can however not think of a reason why Python wouldn't work in single user mode (runlevel 1). – knipknap Jan 03 '10 at 15:05

2 Answers2

5

I put this file nice.py in my site-packages directory (on Windows):

import win32api,win32process,win32con
pid = win32api.GetCurrentProcessId()
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
win32process.SetPriorityClass(handle, win32process.BELOW_NORMAL_PRIORITY_CLASS)

Then I just import nice on any script I want to run at reduced priority.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
  • +1 whether it answers the question or not (whatever the question actually was.. who knows?). This could be quite useful along with `multiprocessing` to spawn off a secondary process that can do background work with minimal impact on the main process for a high-responsiveness application. – Peter Hansen Jan 03 '10 at 17:21
0

Yes. The scripts that control daemons are (normally) plain old bash scripts and can run whatever a bash script can run. The only difference is that in a low runlevel, lots of other system services will not be running, so if the program tries to do something that depends on another daemon, that may fail.

Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96