{"id":1809,"date":"2024-10-10T19:06:52","date_gmt":"2024-10-10T13:36:52","guid":{"rendered":"https:\/\/geekpython.in\/?p=1809"},"modified":"2024-10-10T19:06:53","modified_gmt":"2024-10-10T13:36:53","slug":"how-to-disable-gil-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/how-to-disable-gil-in-python","title":{"rendered":"How to Disable GIL in Python 3.13?"},"content":{"rendered":"\n<p>The official version of Python 3.13 has been released by the Python organization, and with this, you get many major changes.<\/p>\n\n\n\n<p>One of the major changes we get is making <strong>GIL<\/strong> (Global Interpreter Lock) optional in Python 3.13.<\/p>\n\n\n\n<p>In this article, you\u2019ll see how you can disable GIL in Python 3.13 to make multi-threaded programs faster.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Disable GIL in Python 3.13<\/h1>\n\n\n\n<p>First, <a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/www.python.org\/ftp\/python\/3.13.0\/python-3.13.0-amd64.exe\">download<\/a> the official Python 3.13 on your local machine. Then proceed to install the Python executable file.<\/p>\n\n\n\n<p>Follow the steps just as you install any Python version, you will see a step in which you are asked to choose the advanced installation options.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"653\" height=\"402\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-7.png\" alt=\"Advance free-threaded option\" class=\"wp-image-1810\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-7.png 653w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-7-300x185.png 300w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/figure>\n\n\n\n<p>Now select the checkbox appearing at the end, this will install the different executable Python build with the name <code>python3.13t<\/code> or <code>python3.13t.exe<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"653\" height=\"402\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-8.png\" alt=\"Enable free-threaded option\" class=\"wp-image-1811\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-8.png 653w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-8-300x185.png 300w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/figure>\n\n\n\n<p>This will allow you to optionally disable or enable GIL in the runtime. Now proceed to install the setup.<\/p>\n\n\n\n<p>After completing the setup for Python 3.13, if you look at the file location of Python 3.13, you will see that you also got a different Python build called <code>python3.13t<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"448\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-9.png\" alt=\"Free-threaded CPython\" class=\"wp-image-1812\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-9.png 618w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-9-300x217.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/figure>\n\n\n\n<p>If you remember that <strong>\u201cfree-threaded binaries\u201d<\/strong> mode was marked <strong>\u201cexperimental\u201d<\/strong>, this means that you don\u2019t get GIL disabled by default.<\/p>\n\n\n\n<p><strong>But how do I disable GIL? Python organization provided a different CPython build which is also called \u201cFree-threaded CPython\u201d.<\/strong><\/p>\n\n\n\n<p><strong>Free-threaded CPython<\/strong> will help you <strong>disable GIL<\/strong>. Let\u2019s see how to do it.<\/p>\n\n\n\n<p>Let\u2019s say, you have a Python script and you want it to run with GIL disabled. You can use the following command.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >python3.13t main.py<\/pre><\/div>\n\n\n\n<p><strong>This CPython build comes with GIL disabled by default<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practical<\/h2>\n\n\n\n<p>I have the following Python script (<code>main.py<\/code>) that runs a multi-threaded task and calculates the time taken to complete the execution.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >import sys\nimport sysconfig\nimport math\nimport time\nimport threading\n\ndef compute_factorial(n):\n    return math.factorial(n)\n\n# Multi-threaded\ndef multi_threaded_compute(n):\n    threads = []\n    # Create 5 threads\n    for num in n:\n        thread = threading.Thread(target=compute_factorial, args=(num,))\n        threads.append(thread)\n        thread.start()\n\n    # Wait for all threads to complete\n    for thread in threads:\n        thread.join()\n\n    print(\"Factorial Computed.\")\n\ndef main():\n    # Checking Version\n    print(f\"Python version: {sys.version}\")\n\n    # GIL Status\n    status = sysconfig.get_config_var(\"Py_GIL_DISABLED\")\n    if status is None:\n        print(\"GIL cannot be disabled\")\n    if status == 0:\n        print(\"GIL is active\")\n    if status == 1:\n        print(\"GIL is disabled\")\n\n    numlist = [100000, 200000, 300000, 400000, 500000]\n\n    # Multi-threaded Execution\n    start = time.time()\n    multi_threaded_compute(numlist)\n    end = time.time() - start\n    print(f\"Time taken : {end:.2f} seconds\")\n\nif __name__ == \"__main__\":\n    main()<\/pre><\/div>\n\n\n\n<p>Now, we\u2019ll run this script with and without free-threaded CPython.<\/p>\n\n\n\n<p><strong>With GIL enable (without free-threaded CPython)<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >python main.py<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1254\" height=\"179\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-10.png\" alt=\"Python running with GIL enabled\" class=\"wp-image-1813\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-10.png 1254w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-10-300x43.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-10-1024x146.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-10-768x110.png 768w\" sizes=\"auto, (max-width: 1254px) 100vw, 1254px\" \/><\/figure>\n\n\n\n<p><strong>With GIL disable (with free-threaded CPython)<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >python3.13t main.py<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1251\" height=\"203\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-11.png\" alt=\"Python running with GIL disabled\" class=\"wp-image-1814\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-11.png 1251w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-11-300x49.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-11-1024x166.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/10\/image-11-768x125.png 768w\" sizes=\"auto, (max-width: 1251px) 100vw, 1251px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That\u2019s all for now.<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The official version of Python 3.13 has been released by the Python organization, and with this, you get many major changes. One of the major changes we get is making GIL (Global Interpreter Lock) optional in Python 3.13. In this article, you\u2019ll see how you can disable GIL in Python 3.13 to make multi-threaded programs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[2,7],"tags":[31],"class_list":["post-1809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-python-tips","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1809"}],"version-history":[{"count":1,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1809\/revisions"}],"predecessor-version":[{"id":1816,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1809\/revisions\/1816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1815"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}