Beanstalkd can be defined as a work queue. It is used to manage workflow in web applications and increase speed.
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 port beanstalk = 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:
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.
A complete serverside function might look like:
import beanstalkc beanstalk = beanstalkc.Connection(host='localhost', port=20000) while True: job = beanstalk.reserve() #Job should be processed here or in another function job.delete()
All code snippets, except the command-line script, were written in Python.
View all Courses