{"componentChunkName":"component---node-modules-smooth-doc-src-templates-doc-js","path":"/docs/async-await/","result":{"data":{"mdx":{"fields":{"pageType":"doc","title":"async and await","editLink":""},"body":"var _excluded = [\"components\"];\nfunction _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n/* @jsxRuntime classic */\n/* @jsx mdx */\n\nvar _frontmatter = {\n  \"title\": \"async and await\",\n  \"section\": \"Quick Docs\",\n  \"order\": 1\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n    props = _objectWithoutProperties(_ref, _excluded);\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"Python provides the ability to run multiple tasks and coroutines simultaneously with the use of the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"asyncio\"), \" library, which is included in the Python standard library.\"), mdx(\"p\", null, \"This works by running these coroutines in an event loop, where the context of the running coroutine switches periodically to allow all other coroutines to run, thus giving the appearance of running at the same time. This is different to using threads or processes in that all code runs in the main process and thread, although it is possible to run coroutines in other threads.\"), mdx(\"p\", null, \"To call an async function we can either \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"await\"), \" it, or run it in an event loop which we get from \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"asyncio\"), \".\"), mdx(\"p\", null, \"To create a coroutine that can be used with asyncio we need to define a function using the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"async\"), \" keyword:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-py\"\n  }, \"async def main():\\n    await something_awaitable()\\n\")), mdx(\"p\", null, \"Which means we can call \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"await something_awaitable()\"), \" directly from within the function. If this were a non-async function, it would raise the exception \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"SyntaxError: 'await' outside async function\")), mdx(\"p\", null, \"To run the top level async function from outside the event loop we need to use \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://docs.python.org/3/library/asyncio-task.html#asyncio.run\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, \"asyncio.run()\")), \", like this:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-py\"\n  }, \"import asyncio\\n\\nasync def main():\\n    await something_awaitable()\\n\\nasyncio.run(main())\\n\")), mdx(\"p\", null, \"Note that in the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"asyncio.run()\"), \", where we appear to be calling \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"main()\"), \", this does not execute the code in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"main\"), \". Rather, it creates and returns a new \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"coroutine\"), \" object (i.e \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"main() is not main()\"), \") which is then handled and run by the event loop via \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"asyncio.run()\"), \".\"), mdx(\"p\", null, \"To learn more about asyncio and its use, see the \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://docs.python.org/3/library/asyncio.html\"\n  }, \"asyncio documentation\"), \".\"));\n}\n;\nMDXContent.isMDXComponent = true;","tableOfContents":{}}},"pageContext":{"id":"ff4d44b9-0401-5a94-be63-bba43e2ebe6c"}},"staticQueryHashes":["1022386572","1122327541","1339625561","2140385554","3442269102","3597609067"]}