<?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>Forem: Akshay Kaushik</title>
    <description>The latest articles on Forem by Akshay Kaushik (@iamakshaykaushik).</description>
    <link>https://forem.com/iamakshaykaushik</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%2F1227949%2F00a61447-ed9c-4c21-a073-7b41d7b68d3a.png</url>
      <title>Forem: Akshay Kaushik</title>
      <link>https://forem.com/iamakshaykaushik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/iamakshaykaushik"/>
    <language>en</language>
    <item>
      <title>Enhance Linux Performance: Step-by-Step Guide to Increase Swap File Space</title>
      <dc:creator>Akshay Kaushik</dc:creator>
      <pubDate>Fri, 05 Jan 2024 08:25:24 +0000</pubDate>
      <link>https://forem.com/iamakshaykaushik/enhance-linux-performance-step-by-step-guide-to-increase-swap-file-space-4582</link>
      <guid>https://forem.com/iamakshaykaushik/enhance-linux-performance-step-by-step-guide-to-increase-swap-file-space-4582</guid>
      <description>&lt;p&gt;In Linux, swap space allows the system to utilize disk storage for memory management. When the system is running low on physical RAM, it can temporarily move data from memory to swap space on the hard drive.&lt;/p&gt;

&lt;p&gt;Increasing swap file size provides more “breathing room” for memory and can improve system performance when running memory-intensive applications.&lt;/p&gt;

&lt;p&gt;This article explains what swap space is, why you may need to increase it, and a step-by-step process to resize the Linux swap file using the command line.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is swap space?
&lt;/h2&gt;

&lt;p&gt;Think of swap space as a temporary storage area that your computer uses when it's running out of physical memory (RAM). It's like a lifeboat for your system when it's drowning in heavy applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to increase swap space
&lt;/h3&gt;

&lt;p&gt;There are a few common cases when increasing Linux swap space can improve performance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory intensive workloads&lt;/strong&gt;: Applications like data analysis with Pandas, machine learning with NumPy and SciPy, and product analytics with Apache Spark require significant memory. Increasing swap space prevents slow downs when working with large datasets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multitasking with many applications&lt;/strong&gt;: Running multiple applications causes increased memory utilization. More swap space ensures smooth concurrent execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Susceptibility to memory exhaustion&lt;/strong&gt;: Some Linux servers are vulnerable to denial-of-service attacks using up memory. Added swap space gives breathing room to recover safely.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prevent out-of-memory kernel panics&lt;/strong&gt;: In extreme cases, Linux can completely run out of memory and kernel panic. Extra swap prevents this server crash by allowing graceful degradation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check your current swap space utilization to determine if your system needs more swap area on disk.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check current swap usage
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swapon &lt;span class="nt"&gt;--show&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command displays the Linux swap partitions and files currently in use along with their size in kilobytes. It also shows the total swap memory utilization across all swap areas on disk.&lt;/p&gt;

&lt;p&gt;If swap usage is close to 100% or you’re getting out-of-memory errors, increase the swap size.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disable the swap file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;swapoff /name.swap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before resizing an existing swap file, it needs to be temporarily disabled with the swapoff command.&lt;/p&gt;

&lt;p&gt;This stops the OS from writing new swap data so the file can be safely manipulated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a new larger swap file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo dd &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/dev/zero &lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/name.swap.tmp &lt;span class="nv"&gt;bs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1024 &lt;span class="nv"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2097152
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the dd command to quickly create a new 2GB file to use as larger swap space.&lt;/p&gt;

&lt;p&gt;We give this file a temporary name like &lt;code&gt;name.swap.tmp&lt;/code&gt; to avoid overwriting the old swap file before the changeover is complete.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rename and replace the old swap file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo mv&lt;/span&gt; /name.swap /name.swap.old
&lt;span class="nb"&gt;sudo mv&lt;/span&gt; /name.swap.tmp /name.swap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the new swap file is created, rename the original swap file to a backup name like &lt;code&gt;name.swap.old&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then rename the new &lt;code&gt;name.swap.tmp&lt;/code&gt; file to the original swap file name &lt;code&gt;name.swap&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set permissions on the new swap file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo chmod &lt;/span&gt;600 /name.swap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new swap file needs its permissions changed to allow only root read/write access for security reasons. The chmod command does this quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Re-enable the new swap file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;mkswap /name.swap
&lt;span class="nb"&gt;sudo &lt;/span&gt;swapon /name.swap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialize the new swap file using mkswap, then enable it for use with swapon.&lt;/p&gt;

&lt;p&gt;The system can now start utilizing the new, larger swap space.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verify the new swap file size
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swapon &lt;span class="nt"&gt;--show&lt;/span&gt;
free &lt;span class="nt"&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check swapon and free commands to confirm your newly resized swap file is reported correctly.&lt;/p&gt;

&lt;p&gt;You should see the increased swap size along with lower utilization percentage.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reboot the system
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;reboot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After completing the swap file resizing steps, safely reboot Linux to allow services and applications use the added memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQs
&lt;/h2&gt;

&lt;p&gt;1. Why is increasing swap file size beneficial?&lt;br&gt;&lt;br&gt;
- It provides extra 'virtual' memory, which helps your system handle more applications simultaneously.  &lt;/p&gt;

&lt;p&gt;2. Can I increase my swap file size without rebooting the server?&lt;br&gt;&lt;br&gt;
- No, you need to reboot the server for changes to take effect.  &lt;/p&gt;

&lt;p&gt;3. What if my swap space is not showing any increase after following these steps?&lt;br&gt;&lt;br&gt;
- Try disabling and re-enabling the swap space again.  &lt;/p&gt;

&lt;p&gt;4. Is there any limit to how much I can increase my swap file size?&lt;br&gt;&lt;br&gt;
- Yes, it is recommended that your swap space does not exceed 2x the size of your physical RAM.  &lt;/p&gt;

&lt;p&gt;5. Can I decrease my swap file size in the same way?&lt;br&gt;&lt;br&gt;
- Yes, follow these same steps but specify a lower value when resizing the swap file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/iamakshaykaushik/"&gt;I require immediate work. Entry-level is also acceptable.&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Things to Note Before Migrating Servers</title>
      <dc:creator>Akshay Kaushik</dc:creator>
      <pubDate>Wed, 03 Jan 2024 07:39:20 +0000</pubDate>
      <link>https://forem.com/iamakshaykaushik/things-to-note-before-migrating-servers-23o5</link>
      <guid>https://forem.com/iamakshaykaushik/things-to-note-before-migrating-servers-23o5</guid>
      <description>&lt;p&gt;As technology evolves, it becomes necessary for developers to migrate servers. Whether it's upgrading to a more powerful server or downgrading to a more cost-effective solution, the process remains the same.&lt;/p&gt;

&lt;p&gt;Recently, I have migrated my server, holding multiple projects based on WordPress, Nextjs, and Django.&lt;/p&gt;

&lt;p&gt;I am sharing with you the steps that I followed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check the Node/Python Versions
&lt;/h2&gt;

&lt;p&gt;The first step is to document the current Node.js and Python versions running on the existing server. Different applications may require specific major versions, so migrating to a new server with incompatible versions can cause applications to break.&lt;/p&gt;

&lt;p&gt;You can check your Node version using &lt;code&gt;node -v&lt;/code&gt; and Python version with &lt;code&gt;python --version&lt;/code&gt;. Note these versions and make sure your new server can support them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Review requirements.txt and package.json
&lt;/h2&gt;

&lt;p&gt;Dependencies are external modules that your project relies on.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In Python projects, these are listed in a &lt;code&gt;requirements.txt&lt;/code&gt; file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;while in Node. They're in the &lt;code&gt;package.json&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When migrating servers, it's essential to reinstall these dependencies so your application continues to work as intended. Keep a copy of these files and use them to install dependencies on the new server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Migrate Supplementary Services
&lt;/h2&gt;

&lt;p&gt;In addition to the main web frameworks, apps often depend on supplementary services like databases, queues, caches, etc. Common ones include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Relational databases (PostgreSQL, MySQL)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In-memory caches (Redis, Memcached)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Background job queues (Celery, RabbitMQ)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Search indexes (Elasticsearch, Solr)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Document all active services being utilized. During migration, ensure these services are properly set up on the new server and test their functionality before going live.&lt;/p&gt;

&lt;h2&gt;
  
  
  Audit File/Folder Permissions
&lt;/h2&gt;

&lt;p&gt;Server permissions determine who can access what resources on your server. Apps need proper filesystem permissions to startup, read configurations, write logs, etc. Over time, permissions might diverge on an existing server from the defaults.&lt;/p&gt;

&lt;p&gt;Before migrating, audit the permission levels required for key folders like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The application codebase&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Log directories&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Writable temp file areas&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make a list of all users and their permissions from the current server and replicate it on the new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Match the Web Server Version
&lt;/h2&gt;

&lt;p&gt;Web servers like Nginx and Apache turn app code into usable websites and APIs. Confirm the new server is running a compatible web server release.&lt;/p&gt;

&lt;p&gt;For example, Ubuntu runs Apache by default. If apps require Nginx, you may need to manually install a newer version before migrating those sites.&lt;/p&gt;

&lt;p&gt;Also, export and migrate any custom web server configurations that handle things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Custom headers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;URL rewriting&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rate limiting&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Virtual hosts&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adhering to the expected web server version avoids inadvertent issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment File
&lt;/h2&gt;

&lt;p&gt;Environment files (.env) store sensitive information like API keys and database credentials. These files shouldn't be exposed publicly. During migration, don't forget to copy this file and check that it's correctly referenced in your application.&lt;/p&gt;

&lt;p&gt;Ensure all required variables get ported from the old to the new server. Good options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Direct transfer of &lt;code&gt;.env&lt;/code&gt; files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exporting/importing variables from a key/value store&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Test that migrated apps pick up the configurations as expected post-move.&lt;/p&gt;

&lt;h2&gt;
  
  
  Review Cron Jobs
&lt;/h2&gt;

&lt;p&gt;Scheduled cron jobs inevitably get added over time for ops tasks like database backups, log rotations, scheduled jobs, etc.&lt;/p&gt;

&lt;p&gt;Document all cron jobs on the original server before moving. Transfer any custom scripts referenced over to the new server as well.&lt;/p&gt;

&lt;p&gt;After migrating, review that scheduled jobs are firing as expected in the new environment. Troubleshoot any paths/permissions getting in the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Specific Services
&lt;/h2&gt;

&lt;p&gt;Running apps in the background requires using process managers like systemd or PM2 in Node and gunicorn in Python.&lt;/p&gt;

&lt;h3&gt;
  
  
  Gunicorn for Python Projects
&lt;/h3&gt;

&lt;p&gt;Gunicorn acts as a web server gateway interface (WSGI) HTTP server for Python web applications. If your Django projects use Gunicorn, ensure it's properly installed and configured on the new server.&lt;/p&gt;

&lt;h3&gt;
  
  
  PM2 for Node.js Projects
&lt;/h3&gt;

&lt;p&gt;PM2 is a process manager for Node.js applications that allows you to keep applications running forever. If your NextJS projects use PM2, ensure it’s correctly set up on your new server.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Perform Database Migrations&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Database migration is a critical aspect of server migration. Ensure data consistency by correctly backing up your database from the current server and restoring it to the new one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For relational SQL databases, perform a database dump from the old server and import it into the new database. Review for errors and test data integrity post-move.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For in-memory stores like Redis, manually transfer data or leverage built-in migration capabilities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For search indexes like Elasticsearch, many support native snapshotting of indexes to restore new clusters.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Validate that connections exist between migrated apps and databases post-transition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure Load Balancers
&lt;/h2&gt;

&lt;p&gt;In multi-server environments, load balancers distribute requests across app servers. When migrating servers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Add the new server to the load balancer pool but disable traffic to it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Migrate apps to the new server, and validate health checks pass.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gradually shift traffic mix towards the new server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Retire the old server once stable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This ensures no downtime for end users. Review load balancer logs for errors during the migration process.&lt;/p&gt;

&lt;h2&gt;
  
  
  User Acceptance Testing (UAT): Putting the User Experience First
&lt;/h2&gt;

&lt;p&gt;Before switching over to the new server, conduct comprehensive user acceptance testing (UAT) to ensure that all systems and applications are functioning as expected from the user's perspective. This UAT phase should involve testing all critical functionalities, including user authentication, data access, and performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  DNS Cutover and Go-Live: Making the Big Leap
&lt;/h2&gt;

&lt;p&gt;Once UAT is successfully completed, initiate the DNS cutover process, updating DNS records to point to the new server's IP address. Monitor the migration closely during this transition phase to address any potential issues promptly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/iamakshaykaushik/"&gt;I require immediate work. Entry-level is also acceptable.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>server</category>
      <category>node</category>
      <category>django</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>Avoid These 6 Common React useState Hook Mistakes (Code Example)</title>
      <dc:creator>Akshay Kaushik</dc:creator>
      <pubDate>Fri, 29 Dec 2023 16:30:08 +0000</pubDate>
      <link>https://forem.com/iamakshaykaushik/avoid-these-6-common-react-usestate-hook-mistakes-code-example-4kgi</link>
      <guid>https://forem.com/iamakshaykaushik/avoid-these-6-common-react-usestate-hook-mistakes-code-example-4kgi</guid>
      <description>&lt;p&gt;As a React developer, you're probably familiar with the UseState hook. It's a powerful tool that allows you to add state to your functional components. However, it's easy to make mistakes when using it. In this article, we'll explore the most common mistakes and how to avoid them.&lt;/p&gt;

&lt;p&gt;Let's walk through the &lt;strong&gt;6 most common useState mistakes,&lt;/strong&gt; ranging from conceptual errors to subtle syntax issues, and learn how to avoid them through clear examples. Understanding these common pitfalls will level up your React skills.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Not Understanding How useState Works
&lt;/h2&gt;

&lt;p&gt;The useState hook returns an array with 2 elements. The first element is the current state value, while the second element is a state setter function.&lt;/p&gt;

&lt;p&gt;It's important to understand that &lt;strong&gt;useState does not merge object state updates&lt;/strong&gt; - it completely overwrites state.&lt;/p&gt;

&lt;p&gt;View this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; 
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleUpdate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This will overwrite existing state &lt;/span&gt;
    &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/*Renders 'Sam' instead of 'John' after handleUpdate call*/&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;  
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleUpdate&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Update&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To merge state updates correctly, pass an updater function to &lt;code&gt;setState&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Passing Functions Inline
&lt;/h2&gt;

&lt;p&gt;It's convenient to pass functions inline to event handlers or effects, but doing this breaks updating state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Closures prevent this from working&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since function closures are created on each render, &lt;code&gt;count&lt;/code&gt; never updates properly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass functions inline only if they don't update state.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead, declare them outside or in useCallback():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Outside&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In useCallback() &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Forgetting to Handle Arrays and Objects Correctly
&lt;/h2&gt;

&lt;p&gt;Another mistake is mutating state arrays or objects directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTags&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;javascript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Mutates existing array &lt;/span&gt;
  &lt;span class="nx"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nodejs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the &lt;code&gt;tags&lt;/code&gt; array was mutated directly, React can't detect changes and won't trigger re-renders properly.&lt;/p&gt;

&lt;p&gt;Instead, &lt;strong&gt;create a new copy of array/object before passing to setter function&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTags&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nodejs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spread syntax &lt;code&gt;[...array]&lt;/code&gt; shallow copies the array. This lets React detect changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Making Unnecessary useState Calls
&lt;/h2&gt;

&lt;p&gt;There's no need to separate every single field or data point into its own state hook. Doing so hurts performance unnecessarily:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFirstName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLastName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; 
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setFirstName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;  
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setLastName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt; 
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, &lt;strong&gt;group related state into single objects&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFields&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; 
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="c1"&gt;// Call once to update both name fields&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setFields&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; 
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; 
        &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;firstName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; 
        &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lastName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Incorrect State Comparison
&lt;/h2&gt;

&lt;p&gt;When dealing with objects or arrays, avoid direct comparisons for state changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Incorrect&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="c1"&gt;// Correct&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Not Setting a Proper Initial State
&lt;/h2&gt;

&lt;p&gt;It's important to initialize state properly:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Initialize based on props
&lt;/h3&gt;

&lt;p&gt;Rather than hard-coding values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Hard-coded&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="c1"&gt;// Initialise based on props&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Handle unloaded data
&lt;/h3&gt;

&lt;p&gt;If fetching data in &lt;code&gt;useEffect&lt;/code&gt;, set loading state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

&lt;span class="c1"&gt;// Check for null before rendering&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loading...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This avoids errors from rendering undefined state.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Specify data types for state
&lt;/h3&gt;

&lt;p&gt;Typing state helps avoid issues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures you prevent passing an invalid updated state value to setter functions.&lt;/p&gt;

&lt;p&gt;Bookmark this as a handy useState troubleshooting guide. And most importantly, pay special attention to problem areas like spreading arrays before setting state, not mutating objects directly, understanding how useState batching.&lt;/p&gt;

&lt;p&gt;Mastering useState fundamentals takes your React game to the next level!&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Q: Why call a function to update state instead of changing it directly?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; Calling the setter function lets React trigger re-renders. It also queues up state changes for more reliable batching.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: How do you update object state properly?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; Either replace the state completely by passing a new object, or shallow merge by spreading the previous state. Never directly mutate state objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: When should useCallback be used with state setters?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; Use it if passing the setter down in a callback or effect to prevent unnecessary recreations between renders.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: What's the difference between passing a value vs updater function to setState?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; Value replaces state wholly while updater merges by taking previous state as first argument of function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: Why spread arrays/objects before updating state?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; Spreading creates a copied version React can watch for changes, avoiding issues with mutation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/iamakshaykaushik/"&gt;I require immediate work. Entry-level is also acceptable.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>programming</category>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Merge Multiple Git Repositories without Breaking File History</title>
      <dc:creator>Akshay Kaushik</dc:creator>
      <pubDate>Tue, 12 Dec 2023 09:12:52 +0000</pubDate>
      <link>https://forem.com/iamakshaykaushik/merge-multiple-git-repositories-without-breaking-file-history-546m</link>
      <guid>https://forem.com/iamakshaykaushik/merge-multiple-git-repositories-without-breaking-file-history-546m</guid>
      <description>&lt;p&gt;Merging separate Git repositories into a single combined repository is a common need for developers. For example, you may have a frontend and backend repository that are part of the same overall application, but kept in separate repositories during initial development.&lt;/p&gt;

&lt;p&gt;Bringing these components together into a single repository makes it easier to view, manage, and work with the full codebase as a unified project. However, preserving the commit history and being able to trace code changes back to their original repository is also important.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;So, your current setup looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YXXaJttG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3qshua2rthbq64rkgee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YXXaJttG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3qshua2rthbq64rkgee.png" alt="project structure" width="255" height="93"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You've got separate Git repositories for frontend and backend, and now you want to make the Parent Folder the main repository housing both, all while preserving commit histories. Sounds like a plan! Let's dive into the solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to Solve
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Create a New Repository
&lt;/h3&gt;

&lt;p&gt;First things first, let's create a new repository on GitHub (or your preferred hosting service). This will be the main repository for your combined project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/your-username/combined-repo.git
&lt;span class="nb"&gt;cd &lt;/span&gt;combined-repo

&lt;span class="c"&gt;# OR In case of offline&lt;/span&gt;

git init combined-repo
git remote add origin https://github.com/your-username/combined-repo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace "your-username" and "combined-repo" with your GitHub username and the new repository's name. This allows you to pull commits from the source repositories using the remote names as references.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Add Remotes
&lt;/h3&gt;

&lt;p&gt;Now, add remotes for both "frontend" and "backend" repositories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git remote add frontend /path/to/frontend
git remote add backend /path/to/backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace /path/to/frontend and /path/to/backend with the actual paths to your frontend and backend repositories, whether they are offline or online.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Fetch Commits
&lt;/h3&gt;

&lt;p&gt;Fetch the commits from both repositories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git fetch frontend
git fetch backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pulls the branches and associated commits into your local repository without actually merging anything.&lt;/p&gt;

&lt;p&gt;With the commits fetched, you can start merging each repository one at a time.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Merge Frontend
&lt;/h3&gt;

&lt;p&gt;Create and switch to a new branch for frontend:&lt;/p&gt;

&lt;p&gt;First, make a branch for each incoming repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; frontend-merge frontend/master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now merge the branch while allowing unrelated histories between the new repo and old repos:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git merge &lt;span class="nt"&gt;--allow-unrelated-histories&lt;/span&gt; FETCH_HEAD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The --allow-unrelated-histories flag is crucial to allow merging branches that do not share a common starting commit.&lt;/p&gt;

&lt;p&gt;Next, create directories for the frontend, and move the files accordingly from the merged branches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;frontend
git ls-tree &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="nt"&gt;--name-only&lt;/span&gt; HEAD | xargs &lt;span class="nt"&gt;-0&lt;/span&gt; &lt;span class="nt"&gt;-I&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; git &lt;span class="nb"&gt;mv&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; frontend/
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Merge frontend repository into combined-repo"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Commit to each step. Keeping the code organized this way keeps things isolated during additional merging later.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Repeat for Backend
&lt;/h3&gt;

&lt;p&gt;Repeat the process for the backend repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; backend-merge backend/master
git merge &lt;span class="nt"&gt;--allow-unrelated-histories&lt;/span&gt; FETCH_HEAD
&lt;span class="nb"&gt;mkdir &lt;/span&gt;backend
git ls-tree &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="nt"&gt;--name-only&lt;/span&gt; HEAD | xargs &lt;span class="nt"&gt;-0&lt;/span&gt; &lt;span class="nt"&gt;-I&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; git &lt;span class="nb"&gt;mv&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; backend/
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Merge backend repository into combined-repo"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Merge and Resolve Conflicts
&lt;/h3&gt;

&lt;p&gt;Now, let's bring it all together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout master
git merge frontend-merge
git merge backend-merge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As files are brought together from different repositories, merge conflicts can happen just like during normal development.&lt;/p&gt;

&lt;p&gt;Code changes from different sources may modify the same parts of a file in incompatible ways. When this happens, Git stops the merge and flags the conflict.&lt;/p&gt;

&lt;p&gt;If you encounter a "refusing to merge unrelated histories" error, use the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git merge &lt;span class="nt"&gt;--allow-unrelated-histories&lt;/span&gt; frontend-merge
git merge &lt;span class="nt"&gt;--allow-unrelated-histories&lt;/span&gt; backend-merge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resolve any merge conflicts if they occur. Commit the changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Merge frontend-merge and backend-merge into master"&lt;/span&gt;
git push origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! Your GitHub repository's master branch should now contain the combined content and commit history from both frontend and backend repositories.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/iamakshaykaushik/"&gt;I require immediate work. Entry-level is also acceptable.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>webdev</category>
      <category>softwaredevelopment</category>
      <category>github</category>
    </item>
    <item>
      <title>Multithreading VS Multiprocessing VS Asyncio (With Code examples)</title>
      <dc:creator>Akshay Kaushik</dc:creator>
      <pubDate>Sun, 10 Dec 2023 13:15:03 +0000</pubDate>
      <link>https://forem.com/iamakshaykaushik/multithreading-vs-multiprocessing-vs-asyncio-with-code-examples-4166</link>
      <guid>https://forem.com/iamakshaykaushik/multithreading-vs-multiprocessing-vs-asyncio-with-code-examples-4166</guid>
      <description>&lt;p&gt;Concurrency refers to the execution of multiple tasks simultaneously in a program. There are primarily three ways to introduce concurrency in Python - Multithreading, Multiprocessing and Asyncio. Each approach has its advantages and disadvantages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eZUBo2iK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/apipx7gkoaooxrwmfq5y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eZUBo2iK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/apipx7gkoaooxrwmfq5y.jpg" alt="Synchronous Process Example" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choosing the right concurrency model for your Python program depends on the specific requirements and use cases. This comprehensive guide will provide an overview of all three approaches with code examples to help you decide when to use Multithreading, Multiprocessing or Asyncio in Python.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LAilIcoN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1mgesq51holaw9f3rl8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LAilIcoN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1mgesq51holaw9f3rl8.png" alt="When to choose async or multiprocessing or multithreading" width="733" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Multithreading in Python
&lt;/h2&gt;

&lt;p&gt;Multithreading refers to concurrently executing multiple threads within a single process. The Python thread module allows you to spawn multiple threads in your program.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DcQBJiuv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et3xgbxgtq2zk14tmarw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DcQBJiuv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et3xgbxgtq2zk14tmarw.jpg" alt="Multithreading Example" width="694" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is an example code to understand multithreading in Python:&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;import&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt; 

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  Function to print cube of given num
  &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cube: {}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  Function to print square of given num
  &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Square: {}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# creating thread
&lt;/span&gt;    &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;print_square&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,))&lt;/span&gt;  
    &lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;print_cube&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,))&lt;/span&gt;

    &lt;span class="c1"&gt;# starting thread 1
&lt;/span&gt;    &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# starting thread 2
&lt;/span&gt;    &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# wait until thread 1 is completely executed
&lt;/span&gt;    &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# wait until thread 2 is completely executed
&lt;/span&gt;    &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Done!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we create two threads t1 and t2. t1 calls print_square() function while t2 calls print_cube().&lt;/p&gt;

&lt;p&gt;We start both the threads using start() method. join() ensures that the main thread waits for these threads to complete before terminating.&lt;/p&gt;

&lt;p&gt;The key advantage of multithreading is that it allows maximum utilization of a single CPU core by executing threads concurrently. All threads share the same process resources like memory. Context switching between threads is lightweight.&lt;/p&gt;

&lt;p&gt;However, multithreading also comes with challenges like race conditions, deadlocks etc. when multiple threads try to access shared resources. Careful synchronization is needed to avoid these issues.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/IamAkshayKaushik/2544a941d28692ee5982d35ea5403596"&gt;Example of using MultiThreading in Action&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiprocessing in Python
&lt;/h2&gt;

&lt;p&gt;Multiprocessing refers to executing multiple processes concurrently. The multiprocessing module in Python allows spawning processes using an API similar to threading.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e6Xf1LWh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o323joltzuhssikipfvv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e6Xf1LWh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o323joltzuhssikipfvv.jpg" alt="Multiprocessing Python" width="701" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is an example:&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;import&lt;/span&gt; &lt;span class="n"&gt;multiprocessing&lt;/span&gt; 

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  Function to print cube of given num
  &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cube: {}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  Function to print square of given num
  &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Square: {}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# creating process
&lt;/span&gt;    &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;multiprocessing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;print_square&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,))&lt;/span&gt;
    &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;multiprocessing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;print_cube&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,))&lt;/span&gt;

    &lt;span class="c1"&gt;# starting process 1
&lt;/span&gt;    &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# starting process 2
&lt;/span&gt;    &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# wait until process 1 is finished
&lt;/span&gt;    &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# wait until process 2 is finished
&lt;/span&gt;    &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Done!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we create two processes p1 and p2 to execute the print_square() and print_cube() functions concurrently.&lt;/p&gt;

&lt;p&gt;The main difference between multithreading and multiprocessing is that threads run within a single process while processes run independently in separate memory spaces.&lt;/p&gt;

&lt;p&gt;Multiprocessing avoids GIL limitations and allows full utilization of multicore CPUs. However, processes have higher memory overhead compared to threads. Interprocess communication is more complicated compared to thread synchronization primitives.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/IamAkshayKaushik/8d458337358968a7cde5a1683443da87"&gt;Example of MultiProcessing in Action&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Asyncio in Python
&lt;/h2&gt;

&lt;p&gt;Asyncio provides a single-threaded, non-blocking concurrency model in Python. It uses cooperative multitasking and an event loop to execute coroutines concurrently.&lt;/p&gt;

&lt;p&gt;Here is a simple asyncio example:&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;import&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Square: {}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cube: {}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="c1"&gt;# Schedule coroutines to run concurrently
&lt;/span&gt;  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;gather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;print_square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;print_cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define coroutines using async/await syntax. The event loop schedules the execution of coroutines and runs them consecutively.&lt;/p&gt;

&lt;p&gt;Asyncio is best suited for IO-bound tasks and use cases where execution consists of waiting on network responses, database queries etc. It provides high throughput and minimizes blocking.&lt;/p&gt;

&lt;p&gt;However, asyncio doesn't allow true parallellism on multicore systems. CPU-bound processing may suffer performance issues. It has a steep learning curve compared to threads and processes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/a/34681101"&gt;Threading is about workers; asynchrony is about tasks.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Differences
&lt;/h2&gt;

&lt;p&gt;Here is a quick comparison of the three concurrency models:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RjV3wL2P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9ymg0eqkmf1e2jilv0q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RjV3wL2P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9ymg0eqkmf1e2jilv0q.jpg" alt="comparison of the three concurrency models" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use multithreading when you need to run I/O-bound or CPU-bound jobs concurrently in a single process. Examples - serving concurrent requests in a web server, parallel processing in data science apps etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leverage multiprocessing for CPU-bound jobs that require truly parallel execution across multiple cores. Examples - multimedia processing, scientific computations etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asyncio suits network applications like web servers, databases etc. where blocking I/O operations limit performance. Asyncio minimizes blocking for high throughput.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So in summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Multithreading provides concurrent execution in a single process, limited by GIL&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiprocessing provides full parallelism by executing separate processes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asyncio uses an event loop for cooperative multitasking and minimized blocking&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choose the right concurrency model based on your application requirements and use cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Multithreading, multiprocessing and asyncio provide different approaches to concurrency and parallelism in Python.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multithreading uses threads in a single process, multiprocessing spawns separate processes while asyncio leverages an event loop and coroutines for cooperative multitasking.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each model has its strengths and limitations. Understanding the key differences is important to make an informed decision based on the specific problem you are trying to solve.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The code examples and comparisons highlighted in this guide should help you pick the appropriate concurrency framework for your Python programs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Q: What is the Global Interpreter Lock (GIL) in Python?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A: The GIL is a mutex that allows only one thread to execute Python bytecodes at a time even in multi-threaded programs. This prevents multiple threads from running simultaneously on multiple CPU cores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: How can I achieve true parallelism in Python?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A: The multiprocessing module allows spawning multiple processes which can leverage multiple CPUs and cores for parallel execution. It avoids GIL limitations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: When should I use multithreading vs multiprocessing?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A: Use multithreading for I/O bound tasks. Multiprocessing is best for CPU-intensive work that needs parallel speedup across multiple CPU cores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: What are the differences between multiprocessing and multithreading?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A: Key differences are separate vs shared memory, different synchronization primitives, parallelism capabilities etc. Multiprocessing provides full parallelism while multithreading is limited by GIL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: How does asyncio work and when should it be used?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A: Asyncio uses an event loop and coroutines for a cooperative multitasking model suited for I/O bound apps. It minimizes blocking and provides high throughput.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/iamakshaykaushik/"&gt;I require immediate work. Entry-level is also acceptable.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Setting Up Celery for Your Django Project on Ubuntu Server: A Comprehensive Guide</title>
      <dc:creator>Akshay Kaushik</dc:creator>
      <pubDate>Thu, 07 Dec 2023 05:11:20 +0000</pubDate>
      <link>https://forem.com/iamakshaykaushik/setting-up-celery-for-your-django-project-on-ubuntu-server-a-comprehensive-guide-3eec</link>
      <guid>https://forem.com/iamakshaykaushik/setting-up-celery-for-your-django-project-on-ubuntu-server-a-comprehensive-guide-3eec</guid>
      <description>&lt;p&gt;In this tutorial, we'll walk you through the process of configuring Celery, a distributed task queue, with your Django project on an Ubuntu server. Celery enables the asynchronous execution of tasks, enhancing the performance and scalability of your application. We'll break down the configuration of the &lt;code&gt;celeryd&lt;/code&gt; and &lt;code&gt;celery.service&lt;/code&gt; files step by step, helping even novice developers get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Celery
&lt;/h2&gt;

&lt;p&gt;In this section, we will dive into the crucial process of configuring &lt;em&gt;Celery&lt;/em&gt; for your Django project. Celery is a powerful tool that enables you to run asynchronous tasks in the background, enhancing the performance and scalability of your web application.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before we proceed, make sure you have the following tools and prerequisites in place:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A working &lt;strong&gt;Django&lt;/strong&gt; project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;em&gt;Linux&lt;/em&gt; server (in this case, we'll focus on configuring Celery on an &lt;strong&gt;Ubuntu&lt;/strong&gt; server).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Python 3.x&lt;/em&gt; installed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A virtual environment is set up for your Django project.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you haven't set up your Django project or installed Python 3.x, please ensure you do so before continuing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating /etc/default/celeryd
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Section 1:&lt;/strong&gt; &lt;code&gt;celeryd&lt;/code&gt; Configuration&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create the Configuration File:&lt;/strong&gt; To begin, navigate to the &lt;code&gt;/etc/default&lt;/code&gt; directory and create a new file named &lt;code&gt;celeryd&lt;/code&gt; to hold your Celery configuration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Defining Node Configuration:&lt;/strong&gt; Specify the number of nodes for Celery using the &lt;code&gt;CELERYD_NODES&lt;/code&gt; variable. For most setups, a single node is sufficient:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CELERYD_NODES="celery"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Setting Celery Binary Path:&lt;/strong&gt; Define the absolute or relative path to the Celery command using the &lt;code&gt;CELERY_BIN&lt;/code&gt; variable. For example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CELERY_BIN="/home/akshaykaushik.eu.org/AI/.venv/bin/celery"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Specifying Django App Instance:&lt;/strong&gt; Set the Django app instance to be used with Celery using the &lt;code&gt;CELERY_APP&lt;/code&gt; variable, It is the Django project name:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CELERY_APP="Project"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Setting Working Directory:&lt;/strong&gt; Specify the directory where Celery should operate from using the &lt;code&gt;CELERYD_CHDIR&lt;/code&gt; variable(path of directory in which Django manage.py exists):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CELERYD_CHDIR="/home/akshaykaushik.eu.org/AI/"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Configuring Logging:&lt;/strong&gt; Define the logging level and paths for Celery logs using the following variables:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CELERYD_LOG_LEVEL="INFO"
CELERYD_LOG_FILE="/home/akshaykaushik.eu.org/AI/log/celery/%n%I.log"
CELERYD_PID_FILE="/home/akshaykaushik.eu.org/AI/run/celery/%n.pid"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Specifying User and Group:&lt;/strong&gt; Set the unprivileged user and group for Celery workers using the following variables(user and group of the django folder):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CELERYD_USER="aksha1706"
CELERYD_GROUP="aksha1706"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Directory Creation and Permissions:&lt;/strong&gt; Enable directory creation for missing PID and log directories and configure them to be owned by the specified user/group:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CELERY_CREATE_DIRS=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Activating Virtual Environment:&lt;/strong&gt; Specify the virtual environment activation command:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CELERYD_ENV="+/home/akshaykaushik.eu.org/AI/.venv/bin/activate"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Setting Permissions:&lt;/strong&gt; To ensure the Celery worker has the necessary permissions, follow these steps:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Execute the command &lt;code&gt;chmod +x /etc/init.d/celeryd&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;Create the log and PID folders specified in the configuration file and grant them write permissions.
&lt;/li&gt;
&lt;li&gt;With these permissions in place, your Celery worker will operate smoothly.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c"&gt;# most people will only start one node:&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERYD_NODES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"celery"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Absolute or relative path to the 'celery' command:&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERY_BIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/home/akshaykaushik.eu.org/AI/.venv/bin/celery"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# App instance to use&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERY_APP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Project"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Where to chdir at start.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERYD_CHDIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/home/akshaykaushik.eu.org/AI/"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Set logging level to INFO&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERYD_LOG_LEVEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"INFO"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# %n will be replaced with the first part of the node name.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERYD_LOG_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/home/akshaykaushik.eu.org/AI/log/celery/%n%I.log"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERYD_PID_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/home/akshaykaushik.eu.org/AI/run/celery/%n.pid"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Workers should run as an unprivileged user.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERYD_USER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"aksha1706"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERYD_GROUP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"aksha1706"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# If enabled, PID and log directories will be created if missing,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c"&gt;# and owned by the userid/group configured.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERY_CREATE_DIRS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Activate the virtual environment&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;CELERYD_ENV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"+/home/akshaykaushik.eu.org/AI/.venv/bin/activate"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a systemd Service for Celery
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Section 2:&lt;/strong&gt; &lt;code&gt;celery.service&lt;/code&gt; Configuration&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create the Service File:&lt;/strong&gt; Navigate to the &lt;code&gt;/etc/systemd/system&lt;/code&gt; directory and create a new file named &lt;code&gt;celery.service&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Defining the Unit:&lt;/strong&gt; In the &lt;code&gt;celery.service&lt;/code&gt; file, describe the service and set its dependencies:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Unit]
Description=Celery Service
After=network.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Configure the Service:&lt;/strong&gt; Define the service type, user, group, and reference the &lt;code&gt;celeryd&lt;/code&gt; configuration file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Service]
Type=forking
User=aksha1706
Group=aksha1706
EnvironmentFile=/etc/default/celeryd
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Set Working Directory and ExecStart:&lt;/strong&gt; Specify the working directory and the command to start the Celery service:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WorkingDirectory=/home/akshaykaushik.eu.org/AI
ExecStart=/bin/bash -c "source /home/akshaykaushik.eu.org/AI/.venv/bin/activate &amp;amp;&amp;amp; \
  /home/akshaykaushik.eu.org/AI/.venv/bin/celery multi start ${CELERYD_NODES} \
  -A Project --pidfile=${CELERYD_PID_FILE} \
  --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define ExecStop and ExecReload:&lt;/strong&gt; Specify the commands to stop and reload the Celery service:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ExecStop=/bin/bash -c "source /home/akshaykaushik.eu.org/AI/.venv/bin/activate &amp;amp;&amp;amp; \
  /home/akshaykaushik.eu.org/AI/.venv/bin/celery multi stopwait ${CELERYD_NODES} \
  --pidfile=${CELERYD_PID_FILE}"
ExecReload=/bin/bash -c "source /home/akshaykaushik.eu.org/AI/.venv/bin/activate &amp;amp;&amp;amp; \
  /home/akshaykaushik.eu.org/AI/.venv/bin/celery multi restart ${CELERYD_NODES} \
  -A Project --pidfile=${CELERYD_PID_FILE} \
  --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define Installation:&lt;/strong&gt; Specify the target for installation:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Celery&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Service&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;After&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;network.target&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="kr"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;forking&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;aksha1706&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nx"&gt;Group&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;aksha1706&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="n"&gt;EnvironmentFile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;/etc/default/celeryd&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nx"&gt;WorkingDirectory&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;/home/akshaykaushik.eu.org/AI&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nx"&gt;ExecStart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;/bin/bash&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"source /home/akshaykaushik.eu.org/AI/.venv/bin/activate &amp;amp;&amp;amp; \
      /home/akshaykaushik.eu.org/AI/.venv/bin/celery multi start &lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_NODES}&lt;/span&gt;&lt;span class="s2"&gt; \
      -A Project --pidfile=&lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_PID_FILE}&lt;/span&gt;&lt;span class="s2"&gt; \
      --logfile=&lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_LOG_FILE}&lt;/span&gt;&lt;span class="s2"&gt; --loglevel=&lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_LOG_LEVEL}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;ExecStop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;/bin/bash&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"source /home/akshaykaushik.eu.org/AI/.venv/bin/activate &amp;amp;&amp;amp; \
      /home/akshaykaushik.eu.org/AI/.venv/bin/celery multi stopwait &lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_NODES}&lt;/span&gt;&lt;span class="s2"&gt; \
      --pidfile=&lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_PID_FILE}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;ExecReload&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;/bin/bash&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"source /home/akshaykaushik.eu.org/AI/.venv/bin/activate &amp;amp;&amp;amp; \
      /home/akshaykaushik.eu.org/AI/.venv/bin/celery multi restart &lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_NODES}&lt;/span&gt;&lt;span class="s2"&gt; \
      -A Project --pidfile=&lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_PID_FILE}&lt;/span&gt;&lt;span class="s2"&gt; \
      --logfile=&lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_LOG_FILE}&lt;/span&gt;&lt;span class="s2"&gt; --loglevel=&lt;/span&gt;&lt;span class="nv"&gt;${CELERYD_LOG_LEVEL}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Install&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;WantedBy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;multi-user.target&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Enabling and Starting Celery
&lt;/h2&gt;

&lt;p&gt;To activate and initiate Celery as a systemd service, follow these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;sudo systemctl daemon-reload&lt;/code&gt; to reload systemd manager configuration.&lt;/li&gt;
&lt;li&gt;Enable the Celery service: &lt;code&gt;sudo systemctl enable celery&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Start the Celery service: &lt;code&gt;sudo systemctl restart celery&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With these commands, you'll have Celery up and running as a background service.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Celery
&lt;/h2&gt;

&lt;p&gt;To use Celery with your Django project, you need to run it. After activating the virtual environment and navigating to your Django project folder, execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;celery&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-A&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;CELERY_APP_NAME&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-l&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;INFO&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to replace &lt;code&gt;CELERY_APP_NAME&lt;/code&gt; with your actual project name. This command initiates Celery as a worker, ready to process tasks.&lt;/p&gt;

&lt;p&gt;In case you need to make changes or updates, rerun the three commands mentioned above. This ensures that your Celery service remains active and responsive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt; Congratulations! You've successfully configured Celery for your Django project on an Ubuntu server. The &lt;code&gt;celeryd&lt;/code&gt; and &lt;code&gt;celery.service&lt;/code&gt; files are now optimized to handle asynchronous tasks efficiently. By following these detailed steps, even newcomers to development can set up Celery with ease. Don't forget to run the specified commands to finalize the configuration and enjoy enhanced performance and scalability for your Django application. Happy coding!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/iamakshaykaushik/"&gt;I require immediate work. Entry-level is also acceptable.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>ubuntu</category>
      <category>celery</category>
      <category>python</category>
    </item>
  </channel>
</rss>
