<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: lalit-vasoya</title>
    <description>The latest articles on DEV Community by lalit-vasoya (@l_b_vasoya).</description>
    <link>https://dev.to/l_b_vasoya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F352732%2Fe1d8f467-86b1-48cf-9fab-b3d143e2ff9d.jpeg</url>
      <title>DEV Community: lalit-vasoya</title>
      <link>https://dev.to/l_b_vasoya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/l_b_vasoya"/>
    <language>en</language>
    <item>
      <title>Celery, A Must Learn Technology for Python Developers</title>
      <dc:creator>lalit-vasoya</dc:creator>
      <pubDate>Fri, 19 Jun 2020 09:57:27 +0000</pubDate>
      <link>https://dev.to/l_b_vasoya/celery-a-must-learn-technology-for-python-developers-48im</link>
      <guid>https://dev.to/l_b_vasoya/celery-a-must-learn-technology-for-python-developers-48im</guid>
      <description>&lt;p&gt;Here I am trying to cover celery in &lt;code&gt;python&lt;/code&gt;  we need to understand before the use of celery.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;strong&gt;What is celery?&lt;/strong&gt;&lt;br&gt;
Celery is one package or module or program, which is written in python and it helps to divide the program in the peace of task and it will run asynchronous programming or multi-threaded.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Why should we use celery?&lt;/strong&gt;&lt;br&gt;
whenever a user is  trying to book a hotel or a cab, meanwhile he does not get a response for some time from the website, at that time server have been doing his work to get request and process of it  then Return back response to the user, &lt;/p&gt;

&lt;p&gt;when processing time might be increased due to sending confirm hotel, or cab is available then finally confirm it then sending ensuring your cab or hotel book via &lt;code&gt;emails&lt;/code&gt; or &lt;code&gt;SMS&lt;/code&gt; and bla.. ,bla&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K5xR5u4b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pzyb0k22x140dsdyps5u.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K5xR5u4b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pzyb0k22x140dsdyps5u.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we know user stuck, that why we have to define process into multiple task or thread or sub-process and celery help us to do this.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;strong&gt;What is a worker in celery?&lt;/strong&gt;&lt;br&gt;
The worker is like a labor, employee who done his job as per instruction who given by his manager or leader here in celery worker is seem one sub-process that would be done task by assign a message queue. &lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a message queue or broker  in Celery?&lt;/strong&gt;&lt;br&gt;
the broker is the third-person facilitator between a buyer and a seller. In celery. The broker is &lt;code&gt;Redis, rabbitmq&lt;/code&gt; etc who conveying the message in between a client and celery.&lt;/p&gt;

&lt;p&gt;The message queue is one of the managers or leaders.  who receive some instruction from the client and pass instruction on the worker as per his skill so message queue work in celery using a broker like(&lt;code&gt;rabbitmq, Redis, amazon SQS&lt;/code&gt;, etc ) is getting instruction from the client means our project and convey this message to the worker to do those task by assign in our project. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;let's get started our practical &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;We need to do first install our requirements&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Broker or Server&lt;/li&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;Celery &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; :- Here I have used Linux as of now if you have used another platform you should read docs for installation purpose&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install Broker&lt;/strong&gt;&lt;br&gt;
here I am installing &lt;code&gt;rabbitmq&lt;/code&gt;. you could install another broker like &lt;code&gt;redis etc&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
sudo apt-get upgrade
sudo apt-get install rabbitmq-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;restart server and check status of server&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#restart
sudo service rabbitmq-server check status
#status
sudo rabbitmqctl status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Install Python&lt;/strong&gt;&lt;br&gt;
Linux provides python in default. if you have using another platform then you must be &lt;code&gt;install python&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install Celery&lt;/strong&gt;&lt;br&gt;
install in our system level. I have not used &lt;code&gt;virtual-env&lt;/code&gt;.&lt;br&gt;
even you make separate &lt;code&gt;virtual-env&lt;/code&gt; install it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install celery
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now we create file name as tasks.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;celery&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Celery&lt;/span&gt;

&lt;span class="n"&gt;APP_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'tasks'&lt;/span&gt;
&lt;span class="n"&gt;BROKER_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'"amqp://guest:guest@localhost:5672//"'&lt;/span&gt;
&lt;span class="n"&gt;BACKEND&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'db+postgresql://postgres:postgres@localhost/celerydemo'&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Celery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;APP_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;broker&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;BROKER_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;backend&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;BACKEND&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here first we must import &lt;code&gt;Celey&lt;/code&gt; class from &lt;code&gt;celery&lt;/code&gt; and then we define constant &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;APP_NAME&lt;/code&gt; is assigned for unique identify our app might have multiple apps in single projects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;BROKER_NAME&lt;/code&gt; as we had seen what is a broker we need to specify which path use for broker we have used it will provide default when the user is guest if you have created separate user at broker server it will change base on the broker if we execute our task using this &lt;code&gt;BROKER_NAME&lt;/code&gt; thought we communicate with our worker. you could see &lt;a href="https://docs.celeryproject.org/en/stable/userguide/configuration.html"&gt;configure celery&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;BACKEND&lt;/code&gt; is an optional parameter when had passed if we need to store celery task status in the database we have to pass the different path as per database &lt;a href="https://docs.celeryproject.org/en/stable/userguide/configuration.html#database-url-examples"&gt;see path backend in celery&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we open two terminal first for our broker server I have &lt;code&gt;rabbitmq&lt;/code&gt; to interact with celery and give a status of the task and second is &lt;code&gt;python interactive terminal&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pass following command in the first terminal
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;celery -A tasks worker -loglevel=info&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Here we pass argument in &lt;code&gt;celery&lt;/code&gt; command &lt;code&gt;-A&lt;/code&gt; means app then &lt;code&gt;tasks&lt;/code&gt; is the app(same as our file name) we specify and &lt;code&gt;worker&lt;/code&gt; for assign a worker for this task last but not least &lt;code&gt;loglevel&lt;/code&gt; for logging assign it info there is another option available if you &lt;code&gt;--help or -h&lt;/code&gt; for it   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jmSjv2Wa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v39v01nov8d4jmaqthcy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jmSjv2Wa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v39v01nov8d4jmaqthcy.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;open terminal where is your &lt;code&gt;tasks.py&lt;/code&gt; first store and run python
here we start python interactive terminal and import &lt;code&gt;reverse from tasks&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;tasks&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;@&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x7f49adca06d0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'hi there we have used celery'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s"&gt;'yrelec desu evah ew ereht ih'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'hi there we have used celery'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AsyncResult&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;d0f264d0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ec13&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;c60&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="n"&gt;b38&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;064171&lt;/span&gt;&lt;span class="n"&gt;a51a10&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;at that time you will get a task at the first terminal where we have running celery so it will display a log like this&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DCHsilSB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d0i3biwj4ztl12wybqct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DCHsilSB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d0i3biwj4ztl12wybqct.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;we will do a periodic task like every 10-second run task in the next tutorial&lt;/strong&gt; &lt;br&gt;
&lt;br&gt;&lt;br&gt;
hopefully, this tutorial is helpful to you  &lt;/p&gt;

</description>
      <category>python</category>
      <category>subprocess</category>
      <category>celery</category>
      <category>threading</category>
    </item>
  </channel>
</rss>
