{"id":4158,"date":"2022-07-19T02:53:39","date_gmt":"2022-07-19T02:53:39","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4158"},"modified":"2023-06-03T07:23:49","modified_gmt":"2023-06-03T07:23:49","slug":"python-asyncio-future","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-future\/","title":{"rendered":"Python asyncio Future"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python <code>asyncio<\/code> future objects and understand how they work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-asyncio-future'>Introduction to the Python asyncio future <a href=\"#introduction-to-the-python-asyncio-future\" class=\"anchor\" id=\"introduction-to-the-python-asyncio-future\" title=\"Anchor for Introduction to the Python asyncio future\">#<\/a><\/h2>\n\n\n\n<p>A future is an object that returns a value later in the future but not now. Typically, a future object is the result of an asynchronous operation. <\/p>\n\n\n\n<p>For example, you may call an API from a remote server and expect to receive the result later. The API call may return a future object so that you can await it.<\/p>\n\n\n\n<p>To create a future object, you use the <code>Future<\/code> class from the <code>asyncio<\/code> package. Consider the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> asyncio\n<span class=\"hljs-keyword\">from<\/span> asyncio <span class=\"hljs-keyword\">import<\/span> Future\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    my_future = Future()\n    print(my_future.done())  <span class=\"hljs-comment\"># False<\/span>\n\n    my_future.set_result(<span class=\"hljs-string\">'Bright'<\/span>)\n\n    print(my_future.done())  <span class=\"hljs-comment\"># True<\/span>\n\n    print(my_future.result())\n\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, import <code>Future<\/code> class from the <code>asyncio<\/code> library:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> asyncio <span class=\"hljs-keyword\">import<\/span> Future<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, create a new <code>Future<\/code> object in the <code>main()<\/code> coroutine:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_future = Future()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>A newly created future doesn&#8217;t have any value because it doesn&#8217;t exist yet. In this state, the future is considered incomplete, unresolved, or not done. <\/p>\n\n\n\n<p>Then, call the <code>done()<\/code> method to check the status of the future object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(my_future.done())  <span class=\"hljs-comment\"># False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It returns <code>False<\/code>.<\/p>\n\n\n\n<p>After that, set a value for the future object by calling the <code>set_result()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_future.set_result(<span class=\"hljs-string\">'Bright'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Once you set the value, the future is done. Calling the <code>done()<\/code> method of the future object in this stage returns <code>True<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(my_future.done())  <span class=\"hljs-comment\"># True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, get the result from the future object by calling its <code>result()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(my_future.result())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='using-python-asyncio-future-with-await'>Using Python asyncio future with await <a href=\"#using-python-asyncio-future-with-await\" class=\"anchor\" id=\"using-python-asyncio-future-with-await\" title=\"Anchor for Using Python asyncio future with await\">#<\/a><\/h2>\n\n\n\n<p>When you use the <code>await<\/code> keyword with a future, you pause the future until it returns a value. The following example shows how to use the future with <code>await<\/code> keyword:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> asyncio <span class=\"hljs-keyword\">import<\/span> Future\n<span class=\"hljs-keyword\">import<\/span> asyncio\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">plan<\/span><span class=\"hljs-params\">(my_future)<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Planning my future...'<\/span>)\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    my_future.set_result(<span class=\"hljs-string\">'Bright'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">create<\/span><span class=\"hljs-params\">()<\/span> -&gt; Future:<\/span>\n    my_future = Future()\n    asyncio.create_task(plan(my_future))\n    <span class=\"hljs-keyword\">return<\/span> my_future\n\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    my_future = create()\n    result = <span class=\"hljs-keyword\">await<\/span> my_future\n\n    print(result)\n\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Planning my future...\nBright<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a coroutine that accepts a future and sets its value after 1 second:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">plan<\/span><span class=\"hljs-params\">(my_future: Future)<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Planning my future...'<\/span>)\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    my_future.set_result(<span class=\"hljs-string\">'Bright'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define a <code>create()<\/code> function that schedules the <code>plan()<\/code> coroutine as a task and returns a future object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">create<\/span><span class=\"hljs-params\">()<\/span> -&gt; Future:<\/span>\n    my_future = Future()\n    asyncio.create_task(plan(my_future))\n    <span class=\"hljs-keyword\">return<\/span> my_future<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, call the <code>create()<\/code> function that returns a future, use the await keyword to wait for the future to return a result, and display it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    my_future = create()\n    result = <span class=\"hljs-keyword\">await<\/span> my_future\n\n    print(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In practice, you&#8217;ll rarely need to create <code>Future<\/code> objects directly. However, you&#8217;ll use the <code>Future<\/code> objects returned from API. Therefore, it&#8217;s important to understand how the <code>Future<\/code> works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='futures-tasks-and-coroutines'>Futures, Tasks, and Coroutines <a href=\"#futures-tasks-and-coroutines\" class=\"anchor\" id=\"futures-tasks-and-coroutines\" title=\"Anchor for Futures, Tasks, and Coroutines\">#<\/a><\/h2>\n\n\n\n<p>The following class hierarchy shows the relationships between <a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-async-await\/\">Coroutine<\/a>, Future, and <a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-cancel-tasks\/\">Task<\/a>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-future.svg\" alt=\"\" class=\"wp-image-4160\"\/><\/figure>\n<\/div>\n\n\n<p>In this class hierarchy, Courtine, Future, and Task are subclasses of the Awaitable abstract class.<\/p>\n\n\n\n<p>The <code>Awaitable<\/code> class has an abstract method <code><code>__await__()<\/code><\/code>. Any class that has the implementation of the <code><code>__await__()<\/code><\/code> method can be used with the await keyword. And the objects of classes that can be used with the <code>await<\/code> keyword are called <em>awaitables<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A future is an object that returns a value in the future, not now.<\/li>\n\n\n\n<li><code>Future<\/code>, <code>Coroutine<\/code>, and <code>Task<\/code> are awaitable and their objects can be used with the <code>await<\/code> keyword.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4158\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-future\/\"\n\t\t\t\tdata-post-title=\"Python asyncio Future\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4158\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-future\/\"\n\t\t\t\tdata-post-title=\"Python asyncio Future\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about Python asyncio future objects and understand how they work. Introduction to the Python asyncio future # A future is an object that returns a value later in the future but not now. Typically, a future object is the result of an asynchronous operation. For example, you may call [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4104,"menu_order":20,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4158","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4158"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4158\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4104"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}