<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Concurrency on DonDoIT</title><link>/tags/concurrency/</link><description>Recent content in Concurrency on DonDoIT</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Thu, 13 Feb 2025 21:42:52 +0200</lastBuildDate><atom:link href="/tags/concurrency/index.xml" rel="self" type="application/rss+xml"/><item><title>Python_async</title><link>/posts/python/python_async/</link><pubDate>Thu, 13 Feb 2025 21:42:52 +0200</pubDate><guid>/posts/python/python_async/</guid><description>&lt;h1 id="1-is-it-still-single-threaded-when-using-python-asyncawait"&gt;1. Is it still single threaded when using python async/await?&lt;/h1&gt;
&lt;p&gt;Yes!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When an &lt;code&gt;async&lt;/code&gt; function e.g named &lt;code&gt;task1&lt;/code&gt; runs and hits the &lt;code&gt;await&lt;/code&gt; keyword, the event loop &lt;strong&gt;pauses&lt;/strong&gt; the execution of &lt;code&gt;task1&lt;/code&gt; and delegates the work to:
&lt;ul&gt;
&lt;li&gt;OS Kernel (for I/O)&lt;/li&gt;
&lt;li&gt;System timers (for sleep)&lt;/li&gt;
&lt;li&gt;Thread pool (for CPU-bound work, if used explicitly)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;After that, the event loop continues and pick up another &lt;code&gt;async&lt;/code&gt; function in its queue to execute.&lt;/li&gt;
&lt;li&gt;While the event loop is executing other tasks, if the OS finishes working on the delegated job and gives back the result. The result is then put into a &amp;ldquo;result queue&amp;rdquo;.&lt;/li&gt;
&lt;li&gt;When all the &lt;code&gt;async&lt;/code&gt; tasks in the event loop&amp;rsquo;s queue are executed, the event loop will pick up the results from the results queue, in the order of first come first serve.&lt;/li&gt;
&lt;li&gt;The event loop will now continue the execution of the task of first result that gets pickup, and continue until it hits another &lt;code&gt;await&lt;/code&gt; or finished the execution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Async functions (&lt;code&gt;async def&lt;/code&gt;) run in the same thread&lt;/strong&gt; and do not block the event loop as long as they contain &lt;code&gt;await&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Regular (&lt;code&gt;def&lt;/code&gt;) functions block the event loop&lt;/strong&gt; unless explicitly run in a separate thread or process.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id="2-what-kind-of-tasks-would-be-best-to-use-asyncawait"&gt;2. What kind of tasks would be best to use &lt;code&gt;async/await&lt;/code&gt;?&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Things like I/O, sleep, network requests, file reads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Best practice:&lt;/strong&gt; Keep async functions fully async and offload blocking operations to threads or processes when necessary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id="3-prevent-blocking"&gt;3. Prevent blocking?&lt;/h1&gt;
&lt;p&gt;To prevent blocking, use:&lt;/p&gt;</description><content>&lt;h1 id="1-is-it-still-single-threaded-when-using-python-asyncawait"&gt;1. Is it still single threaded when using python async/await?&lt;/h1&gt;
&lt;p&gt;Yes!&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When an &lt;code&gt;async&lt;/code&gt; function e.g named &lt;code&gt;task1&lt;/code&gt; runs and hits the &lt;code&gt;await&lt;/code&gt; keyword, the event loop &lt;strong&gt;pauses&lt;/strong&gt; the execution of &lt;code&gt;task1&lt;/code&gt; and delegates the work to:
&lt;ul&gt;
&lt;li&gt;OS Kernel (for I/O)&lt;/li&gt;
&lt;li&gt;System timers (for sleep)&lt;/li&gt;
&lt;li&gt;Thread pool (for CPU-bound work, if used explicitly)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;After that, the event loop continues and pick up another &lt;code&gt;async&lt;/code&gt; function in its queue to execute.&lt;/li&gt;
&lt;li&gt;While the event loop is executing other tasks, if the OS finishes working on the delegated job and gives back the result. The result is then put into a &amp;ldquo;result queue&amp;rdquo;.&lt;/li&gt;
&lt;li&gt;When all the &lt;code&gt;async&lt;/code&gt; tasks in the event loop&amp;rsquo;s queue are executed, the event loop will pick up the results from the results queue, in the order of first come first serve.&lt;/li&gt;
&lt;li&gt;The event loop will now continue the execution of the task of first result that gets pickup, and continue until it hits another &lt;code&gt;await&lt;/code&gt; or finished the execution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Async functions (&lt;code&gt;async def&lt;/code&gt;) run in the same thread&lt;/strong&gt; and do not block the event loop as long as they contain &lt;code&gt;await&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Regular (&lt;code&gt;def&lt;/code&gt;) functions block the event loop&lt;/strong&gt; unless explicitly run in a separate thread or process.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id="2-what-kind-of-tasks-would-be-best-to-use-asyncawait"&gt;2. What kind of tasks would be best to use &lt;code&gt;async/await&lt;/code&gt;?&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Things like I/O, sleep, network requests, file reads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Best practice:&lt;/strong&gt; Keep async functions fully async and offload blocking operations to threads or processes when necessary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id="3-prevent-blocking"&gt;3. Prevent blocking?&lt;/h1&gt;
&lt;p&gt;To prevent blocking, use:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;asyncio.to_thread()&lt;/code&gt;&lt;/strong&gt; → Runs a regular function in a separate thread (good for I/O).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;asyncio.run_in_executor()&lt;/code&gt; with &lt;code&gt;ProcessPoolExecutor&lt;/code&gt;&lt;/strong&gt; → Runs CPU-heavy tasks in a separate process (avoids GIL limitations).&lt;/li&gt;
&lt;/ul&gt;</content></item></channel></rss>