pystalk documentation¶
pystalk is a simple Python module for interacting with the beanstalk task queueing daemon. It doesn’t provide much magic, but is suitable for building all sorts of functionality on top of.
This work is available under the terms of the ISC License.
See also
Contents¶
Members¶
- class pystalk.BeanstalkClient(host: str, port: int = 11300, socket_timeout: float | None = None, auto_decode: bool = False)[source]¶
Simple wrapper around the Beanstalk API.
- Parameters:
Doesn’t provide any fanciness for writing consumers or producers. Just lets you invoke methods to call beanstalk functions.
Warning
Setting socket timeout to a value lower than the value you pass to blocking functions like
reserve_job()will cause errors!- bury_job(job_id, pri=65536)[source]¶
Mark the given job_id as buried. The job must have been previously reserved by this connection
- Parameters:
job_id – Job to bury
pri (int) – Priority for the newly-buried job. If not passed, will keep its current priority
- close()[source]¶
Close any open connection to the Beanstalk server.
This object is still safe to use after calling
close(); it will automatically reconnect and re-establish any open watches / uses.It is a logic error to close the connection while you have a reserved job
- delete_job(job_id)[source]¶
Delete the given job id. The job must have been previously reserved by this connection
- classmethod from_uri(uri, socket_timeout=None, auto_decode=False)[source]¶
Construct a synchronous Beanstalk Client from a URI.
The URI may be of the form beanstalk://host:port or beanstalkd://host:port
IPv6 literals must be wrapped in brackets as per RFC 2732.
- ignore(tube)[source]¶
Remove the given tube from the watchlist.
- Parameters:
tube – Name of tube to remove from the watchlist
If all tubes are
ignore()d, beanstalk will auto-add “default” to the watchlist to prevent the list from being empty. Seewatch()for more unformation.
- kick_job(job_id)[source]¶
Kick the given job id. The job must either be in the DELAYED or BURIED state and will be immediately moved to the READY state.
- kick_jobs(num_jobs)[source]¶
Kick some number of jobs from the buried queue onto the ready queue.
- Parameters:
num_jobs (int) – Number of jobs to kick
If not that many jobs are in the buried queue, it will kick as many as it can.
- list_tubes()[source]¶
Return a list of tubes that this beanstalk instance knows about
- Return type:
list of tubes
- pause_tube(tube, delay=3600)[source]¶
Pause a tube for some number of seconds, preventing it from issuing jobs.
- Parameters:
delay (int) – Time to pause for, in seconds
There is no way to permanently pause a tube; passing 0 for delay actually un-pauses the tube.
See also
- put_job(data: str | bytes, pri: int = 65536, delay: int = 0, ttr: int = 120)[source]¶
Insert a new job into whatever queue is currently USEd
- Parameters:
data (Text (either str which will be encoded as utf-8, or bytes which are already utf-8) – Job body
pri (int) – Priority for the job
delay (int) – Delay in seconds before the job should be placed on the ready queue
ttr (int) – Time to reserve (how long a worker may work on this job before we assume the worker is blocked and give the job to another worker
See also
put_job_into()Put a job into a specific tube
using()Insert a job using an external guard
- put_job_into(tube_name: str, data: str | bytes, pri: int = 65536, delay: int = 0, ttr: int = 120)[source]¶
Insert a new job into a specific queue. Wrapper around
put_job().- Parameters:
tube_name (str) – Tube name
data (Text (either str which will be encoded as utf-8, or bytes which are already utf-8) – Job body
pri (int) – Priority for the job
delay (int) – Delay in seconds before the job should be placed on the ready queue
ttr (int) – Time to reserve (how long a worker may work on this job before we assume the worker is blocked and give the job to another worker
- release_job(job_id, pri=65536, delay=0)[source]¶
Put a job back on the queue to be processed (indicating that you’ve aborted it)
You can only release a job which you have reserved using
reserve_job()orreserve_iter().
- reserve_iter()[source]¶
Reserve jobs as an iterator. Ends iteration when there are no more jobs immediately available
- reserve_job(timeout=5)[source]¶
Reserve a job for this connection. Blocks for TIMEOUT secionds and raises TIMED_OUT if no job was available
- Parameters:
timeout (int) – Time to wait for a job, in seconds.
- stats_tube(tube_name)[source]¶
Fetch statistics about a single tube
- Parameters:
tube_name – Tube to fetch stats about
- Return type:
- unpause_tube(tube)[source]¶
Unpause a tube which was previously paused with
pause_tube().See also
- use(tube)[source]¶
Start producing jobs into the given tube.
- Parameters:
tube – Name of the tube to USE
Subsequent calls to
put_job()insert jobs into this tube.
- using(tube)[source]¶
Context-manager to insert jobs into a specific tube
- Parameters:
tube – Tube to insert to
Yields out an instance of
BeanstalkInsertingProxyto insert items into that tubeSee also
use()Change the default tube
put_job()Put a job into whatever the current tube is
put_job_into()Put a job into a specific tube
- watch(tube)[source]¶
Add the given tube to the watchlist.
- Parameters:
tube – Name of the tube to add to the watchlist
Note: Initially, all connections are watching a tube named “default”. If you manually call
watch(), we will un-watch the “default” tube. To keep it in your list, first callwatch()with the other tubes, then callwatch()with “default”.
- property watchlist¶
- class pystalk.BeanstalkConnectionError(host, port, err)[source]¶
Raised when the underlying socket connection to beanstalkd fails.
Distinguishes connection-level failures (e.g. connection refused, timeout, DNS errors) from beanstalk protocol errors. Carries the
hostand port that were being connected to, and preserves the original socket error both onerrand via implicit exception chaining (__context__).This is a subclass of
BeanstalkErrorso that existingexcept BeanstalkErrorcallers continue to catch connection failures.
- class pystalk.client.Job(job_id, job_data)[source]¶
Structure holding a job returned from Beanstalk
- Variables:
job_id – Opaque identifier for the job (to be passed to
BeanstalkClient.release_job()orBeanstalkClient.stats_job()).job_data – Blob of the data. str if
BeanstalkClient.auto_decodeis True; otherwise bytes
- class pystalk.client.BeanstalkInsertingProxy(beanstalk_client: BeanstalkClient, tube: str)[source]¶
Proxy object yielded by
BeanstalkClient.using()- put_job(data: str | bytes, pri: int = 65536, delay: int = 0, ttr: int = 120)[source]¶
Method to insert a job into the tube selected with
BeanstalkClient.using().- Parameters:
data (Text (either str which will be encoded as utf-8, or bytes which are already utf-8) – Job body
pri (int) – Priority for the job
delay (int) – Delay in seconds before the job should be placed on the ready queue
ttr (int) – Time to reserve (how long a worker may work on this job before we assume the worker is blocked and give the job to another worker