<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ayush</title>
    <description>The latest articles on DEV Community by Ayush (@adayush).</description>
    <link>https://dev.to/adayush</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%2F1202852%2F0f5be6e4-6147-4664-a594-a459ca1c27c0.jpeg</url>
      <title>DEV Community: Ayush</title>
      <link>https://dev.to/adayush</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adayush"/>
    <language>en</language>
    <item>
      <title>Getting Started with OLA Maps Python package</title>
      <dc:creator>Ayush</dc:creator>
      <pubDate>Thu, 11 Jul 2024 14:57:29 +0000</pubDate>
      <link>https://dev.to/adayush/getting-started-with-ola-maps-python-package-1j4m</link>
      <guid>https://dev.to/adayush/getting-started-with-ola-maps-python-package-1j4m</guid>
      <description>&lt;p&gt;Recently OLA announced their new &lt;a href="https://maps.olakrutrim.com/" rel="noopener noreferrer"&gt;Maps platform&lt;/a&gt; and they're giving it away for free for a year. If you're planning to use it in your project, I've built a new Python package that makes it easy to integrate OLA Maps functionality into your Python projects. Let's explore how to use this package.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;First, install the package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;olamaps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Authentication
&lt;/h2&gt;

&lt;p&gt;Before you can use the OLA Maps API, you need to authenticate. The package supports two methods:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using an API key:
&lt;/li&gt;
&lt;/ol&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;os&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;OLAMAPS_API_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_api_key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;# OR
&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_api_key_here&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;ol&gt;
&lt;li&gt;Using client ID and client secret:
&lt;/li&gt;
&lt;/ol&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;os&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;OLAMAPS_CLIENT_ID&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_client_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;OLAMAPS_CLIENT_SECRET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_client_secret&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;# OR
&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_client_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client_secret&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_client_secret&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;h2&gt;
  
  
  Basic Usage
&lt;/h2&gt;

&lt;p&gt;Here's how to use the main features of the package:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="c1"&gt;# Initialize the client
&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Autocomplete a query
&lt;/span&gt;&lt;span class="n"&gt;autocomplete_results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;autocomplete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Kempe&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Geocode a text address
&lt;/span&gt;&lt;span class="n"&gt;geocode_results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;geocode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;MG Road, Bangalore&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Reverse geocode a latitude-longitude pair
&lt;/span&gt;&lt;span class="n"&gt;reverse_geocode_results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse_geocode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;12.9519408&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;lng&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;77.6381845&lt;/span&gt;&lt;span class="sh"&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;# Get directions
&lt;/span&gt;&lt;span class="n"&gt;directions_results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;directions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;origin&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;12.993103152916301,77.54332622119354&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;12.972006793201695,77.5800850011884&lt;/span&gt;&lt;span class="sh"&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;# close the client
&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The package also supports an &lt;code&gt;AsyncClient&lt;/code&gt; and context managers. Check out the docs for them &lt;a href="https://github.com/adayush/olamaps-python?tab=readme-ov-file#client" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The olamaps package provides a simple way to integrate OLA Maps functionality into your Python projects. Whether you need to geocode addresses, reverse geocode coordinates, or get directions, this package has you covered.&lt;/p&gt;

&lt;p&gt;Find this project on &lt;a href="https://pypi.org/project/olamaps/" rel="noopener noreferrer"&gt;PyPI&lt;/a&gt; and on &lt;a href="https://github.com/adayush/olamaps-python" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; (Would love some ⭐️)&lt;/p&gt;

&lt;p&gt;Remember, this is an unofficial package and is not endorsed by OLA. Always make sure you comply with OLA's terms of service when using their API.&lt;/p&gt;

&lt;p&gt;Happy mapping!&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>FastAPI - Concurrency in Python</title>
      <dc:creator>Ayush</dc:creator>
      <pubDate>Sat, 29 Jun 2024 06:18:28 +0000</pubDate>
      <link>https://dev.to/adayush/fastapi-concurrency-in-python-29e3</link>
      <guid>https://dev.to/adayush/fastapi-concurrency-in-python-29e3</guid>
      <description>&lt;p&gt;Recently I delved deep into FastAPI docs and some other resources to understand how each route is processed and what can be done to optimise FastAPI for scale. This is about the learnings I gathered.&lt;/p&gt;




&lt;h2&gt;
  
  
  A little refresher
&lt;/h2&gt;

&lt;p&gt;Before we go into optimising FastAPI, I'd like to give a short tour on a few technical concepts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Threads &amp;amp; Processes
&lt;/h3&gt;

&lt;p&gt;Threads share memory space and are easier to create.&lt;br&gt;
Processes have separate memory space and thus require some overhead to create.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multi-threading &amp;amp; Multi-processing
&lt;/h3&gt;

&lt;p&gt;Multi-threading uses multiple threads within a single process.&lt;br&gt;
Multi-processing utilizes multiple processes, leveraging multiple CPU cores.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concurrency &amp;amp; Parallelism
&lt;/h3&gt;

&lt;p&gt;Concurrency is managing multiple tasks together. Made possible using event loop.&lt;br&gt;
Parallelism is executing multiple tasks simultaneously. Made possible using multiple CPU-cores.&lt;/p&gt;

&lt;h3&gt;
  
  
  Python &amp;amp; the GIL
&lt;/h3&gt;

&lt;p&gt;Python's Global Interpreter Lock (GIL) allows only one thread to execute Python bytecode at a time. However, for I/O-bound operations, the GIL is released, allowing other threads to run while waiting for I/O completion. This makes Python particularly effective for I/O-heavy applications.&lt;/p&gt;

&lt;p&gt;Quick FYI: In some time, GIL will probably be optional in Python. (&lt;a href="https://peps.python.org/pep-0703/" rel="noopener noreferrer"&gt;PEP 703&lt;/a&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  FastAPI
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How route handlers are processed
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Regular route handlers run in an external threadpool&lt;/li&gt;
&lt;li&gt;Async route handlers run in the main thread&lt;/li&gt;
&lt;li&gt;FastAPI doesn't affect other (utility) functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Optimizing FastAPI
&lt;/h3&gt;

&lt;p&gt;Choose based on your task:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I/O tasks with minimal or no CPU work: Use async route handler and await I/O tasks&lt;/li&gt;
&lt;li&gt;Non-async I/O tasks: use regular (def) route handler&lt;/li&gt;
&lt;li&gt;I/O tasks with significant CPU work: Use regular route handler or async route handler which queues the task for external worker (multi-processing)&lt;/li&gt;
&lt;li&gt;High compute tasks: Use multi-processing same as above&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The reason I'm suggesting to use regular route handlers for most cases is because we want to keep the main thread available for receiving requests and managing them. If we have any blocking code in main thread (async handlers) it would affect the incoming requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using multiple processes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For containerized environments: Use Docker Swarm/Kubernetes to create workers and use a load balancer&lt;/li&gt;
&lt;li&gt;For traditional setups or just Docker: Use Gunicorn (for process management) with Uvicorn (workers)&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  Some resources I found to be of great help
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=HTSK6eRwyGM" rel="noopener noreferrer"&gt;Building large APIs with FastAPI - PyCon SK&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=AZnGRKFUU0c" rel="noopener noreferrer"&gt;Threading vs multiprocessing in python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://fastapi.tiangolo.com/async/" rel="noopener noreferrer"&gt;https://fastapi.tiangolo.com/async/&lt;/a&gt; (official FastAPI docs, they're amazing)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>fastapi</category>
    </item>
  </channel>
</rss>
