This document describes the current stable version of Celery (3.1). For development docs, go here.

celery.platforms

celery.platforms

Utilities dealing with platform specifics: signals, daemonization, users, groups, and so on.

celery.platforms.pyimplementation()[source]

Return string identifying the current Python implementation.

exception celery.platforms.LockFailed[source]

Raised if a pidlock can’t be acquired.

celery.platforms.get_fdmax(default=None)[source]

Return the maximum number of open file descriptors on this system.

Parameters:default – Value returned if there’s no file descriptor limit.
class celery.platforms.Pidfile(path)[source]

Pidfile

This is the type returned by create_pidlock().

TIP: Use the create_pidlock() function instead, which is more convenient and also removes stale pidfiles (when the process holding the lock is no longer running).

acquire()[source]

Acquire lock.

is_locked()[source]

Return true if the pid lock exists.

path = None
read_pid()[source]

Read and return the current pid.

release(*args)[source]

Release lock.

remove()[source]

Remove the lock.

remove_if_stale()[source]

Remove the lock if the process is not running. (does not respond to signals).

write_pid()[source]
celery.platforms.create_pidlock(pidfile)[source]

Create and verify pidfile.

If the pidfile already exists the program exits with an error message, however if the process it refers to is not running anymore, the pidfile is deleted and the program continues.

This function will automatically install an atexit handler to release the lock at exit, you can skip this by calling _create_pidlock() instead.

Returns:Pidfile.

Example:

pidlock = create_pidlock('/var/run/app.pid')
celery.platforms.close_open_fds(keep=None)[source]
class celery.platforms.DaemonContext(pidfile=None, workdir=None, umask=None, fake=False, after_chdir=None, **kwargs)[source]
close(*args)[source]
open()[source]
redirect_to_null(fd)[source]
celery.platforms.detached(logfile=None, pidfile=None, uid=None, gid=None, umask=0, workdir=None, fake=False, **opts)[source]

Detach the current process in the background (daemonize).

Parameters:
  • logfile – Optional log file. The ability to write to this file will be verified before the process is detached.
  • pidfile – Optional pidfile. The pidfile will not be created, as this is the responsibility of the child. But the process will exit if the pid lock exists and the pid written is still running.
  • uid – Optional user id or user name to change effective privileges to.
  • gid – Optional group id or group name to change effective privileges to.
  • umask – Optional umask that will be effective in the child process.
  • workdir – Optional new working directory.
  • fake – Don’t actually detach, intented for debugging purposes.
  • **opts – Ignored.

Example:

from celery.platforms import detached, create_pidlock

with detached(logfile='/var/log/app.log', pidfile='/var/run/app.pid',
              uid='nobody'):
    # Now in detached child process with effective user set to nobody,
    # and we know that our logfile can be written to, and that
    # the pidfile is not locked.
    pidlock = create_pidlock('/var/run/app.pid')

    # Run the program
    program.run(logfile='/var/log/app.log')
celery.platforms.parse_uid(uid)[source]

Parse user id.

uid can be an integer (uid) or a string (user name), if a user name the uid is taken from the system user registry.

celery.platforms.parse_gid(gid)[source]

Parse group id.

gid can be an integer (gid) or a string (group name), if a group name the gid is taken from the system group registry.

celery.platforms.setgroups(groups)[source]

Set active groups from a list of group ids.

celery.platforms.initgroups(uid, gid)[source]

Compat version of os.initgroups() which was first added to Python 2.7.

celery.platforms.setgid(gid)[source]

Version of os.setgid() supporting group names.

celery.platforms.setuid(uid)[source]

Version of os.setuid() supporting usernames.

celery.platforms.maybe_drop_privileges(uid=None, gid=None)[source]

Change process privileges to new user/group.

If UID and GID is specified, the real user/group is changed.

If only UID is specified, the real user is changed, and the group is changed to the users primary group.

If only GID is specified, only the group is changed.

celery.platforms.set_process_title(progname, info=None)[source]

Set the ps name for the currently running process.

Only works if setproctitle is installed.

celery.platforms.set_mp_process_title(progname, info=None, hostname=None)[source]

Set the ps name using the multiprocessing process name.

Only works if setproctitle is installed.

celery.platforms.get_errno_name(n)[source]

Get errno for string, e.g. ENOENT.

celery.platforms.ignore_errno(*args, **kwds)[source]

Context manager to ignore specific POSIX error codes.

Takes a list of error codes to ignore, which can be either the name of the code, or the code integer itself:

>>> with ignore_errno('ENOENT'):
...     with open('foo', 'r') as fh:
...         return fh.read()

>>> with ignore_errno(errno.ENOENT, errno.EPERM):
...    pass
Parameters:types – A tuple of exceptions to ignore (when the errno matches), defaults to Exception.

Previous topic

celery.utils.dispatch.saferef

Next topic

celery._state

This Page