What is Beanstalkd?
Beanstalkd
Beanstalkd can be defined as a work queue. It is used to manage workflow in web applications and increase speed.
Uses
- Jobs can be divided among multiple computers.
- Certain jobs can be given priority over others.
- Jobs that need to be done at specific intervals can be done as required.
- Disconnected clients can be allowed to return without losing their progress.
Basics
To initialize Beanstalkd, it needs to connect and listen on a port. This operation can be done using a command-line operation:
beanstalkd -l 127.0.0.1 -p 20000
This command sets up Beanstalkd to listen at port 20000 on the host system. Next, Beanstalkd needs to be listening on this port in the program handling it as well:
import beanstalkc# Connect beanstalk to thd correct ip address and portbeanstalk = beanstalkc.Connection(host = 'localhost', port = 20000)
This code snippet initializes a beanstalk client on localhost to listen on port 20000.
Jobs can be inserted into the queue using the put function:
beanstalk.put('job')
As demonstrated in the example above, jobs need to be in string form to be inserted into the work queue.
To retrieve a job, the reserve function can be used:
job = beanstalk.reserve()
After processing, jobs need to be deleted. If they are not deleted, they are re-inserted into the queue after a certain amount of time.
job.delete()
A complete serverside function might look like:
import beanstalkcbeanstalk = beanstalkc.Connection(host='localhost', port=20000)while True:job = beanstalk.reserve()#Job should be processed here or in another functionjob.delete()
All code snippets, except the command-line script, were written in Python.
Free Resources