Skip to content

Commit

Permalink
Worker and child processes now get named
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Nov 28, 2009
1 parent aa50f40 commit b5061ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dreque/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
try:
import procname
setprocname = procname.setprocname
getprocname = procname.getprocname
except ImportError:
try:
from ctypes import cdll, byref, create_string_buffer
libc = cdll.LoadLibrary('libc.so.6')
def setprocname(name):
buff = create_string_buffer(len(name)+1)
buff.value = name
libc.prctl(15, byref(buff), 0, 0, 0)
# FreeBSD: libc.setproctitle(name)
def getprocname():
libc = cdll.LoadLibrary('libc.so.6')
buff = create_string_buffer(128)
# 16 == PR_GET_NAME from <linux/prctl.h>
libc.prctl(16, byref(buff), 0, 0, 0)
return buff.value
except (OSError, ImportError):
def setprocname(name):
pass
def getprocname():
import sys
return sys.argv[0]
3 changes: 3 additions & 0 deletions dreque/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import socket
import time
from dreque.base import Dreque
from dreque.utils import setprocname

class DrequeWorker(Dreque):
def __init__(self, queues, server, db=None):
Expand Down Expand Up @@ -55,12 +56,14 @@ def process(self, job):

p = Process(target=self.dispatch, args=(job,))
p.start()
setprocname("dreque: Forked %d at %d" % (p.pid, time.time()))
p.join()

if p.exitcode != 0:
raise Exception("Job failed")

def dispatch(self, job):
setprocname("dreque: Processing %s since %d" % (job['queue'], time.time()))
func = self.lookup_function(job['func'])
kwargs = dict((str(k), v) for k, v in job['kwargs'].items())
func(*job['args'], **kwargs)
Expand Down

0 comments on commit b5061ab

Please sign in to comment.