Beanstalkd in Python
Beanstalkd is a work queue server that runs time-consuming tasks behind the scene. It receives messages/jobs from producers/senders and processes them with consumers/receivers.
get data from beanstalk or other message queue called consume data, and engine or coding to consume is called consumer, its like a produce or send data to beanstalk.
im using beanstalkd as Ubuntu 20.04, vscode as text editor.
How to install beanstalkd in ubuntu:
insert this code to your terminal$ sudo apt-get update
$ sudo apt-get install beanstalkd
Command to start, stop, restart or check beanstalkd
$ sudo systemctl {start|status|restart|stop} beanstalkd
or
$ sudo service beanstalkd {start|status|restart|stop}
How To Install Web Interface for Beanstalkd
$ curl -s http://getcomposer.org/installer | php
$ composer create-project ptrofimov/beanstalk_console -s dev /var/www/html/beanstalk_console
$ cd /var/www/html/beanstalk_console
Check your Hostname in beanstalkd
$ hostname -Iresult (192.168.1.104 172.18.0.1 172.17.0.1)$ php -S 192.168.1.104:7654 -t public
Access on your browser using hostname and port like a 192.168.1.104:7654
add server with localhost:11300 (beanstalkd local)
Congrats, yout Beanstalk has been installed!.
Lets Coding
Requirement
Python 3
pystalk library
pystalk is a python library to access beanstalkd on local or server with simple code. first install this library with pip
pip install pystalk
and now i’ll make a producer with python.
#PRODUCERfrom pystalk import BeanstalkClientimport jsonclient = BeanstalkClient('127.0.0.1', 11300)dicts = {'name':"nasri adzlani",'job':'data engineer','studied':'yogyakarta Techology of University'}with client.using("anjay") as inserter: inserter.put_job(json.dumps(dicts))
im send data dicts to beanstalkd in job “anjay” u can change job name.
#CONSUMERfrom pystalk import BeanstalkClientimport jsonclient = BeanstalkClient('127.0.0.1', 11300)client.watch("anjay")for job in client.reserve_iter(): print(job.job_data)
save and run this code, using terminal command command
python3 <filename>.py
example output
b'{"name": "nasri adzlani", "job": "data engineer", "studied": "yogyakarta Techology of University"}'
consumer data type is a bytes, couse it is default data type in beanstalkd. u can parsing this data to anything u want.
So simple code, i want u happy with this article.