{"id":115154,"date":"2021-12-13T11:25:40","date_gmt":"2021-12-13T16:25:40","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=115154"},"modified":"2022-11-21T09:50:01","modified_gmt":"2022-11-21T14:50:01","slug":"how-to-stop-long-running-code-in-python","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/","title":{"rendered":"How to Stop Long-running Code in Python"},"content":{"rendered":"\n<p>Ever had long-running code that you don\u2019t know when it\u2019s going to finish running? If you have, then Python\u2019s&nbsp;<strong>stopit<\/strong>&nbsp;library is for you. In a previous post, we talked about&nbsp;<a href=\"https:\/\/theautomatic.net\/2020\/10\/12\/how-to-create-a-progress-bar-in-python\/\">how to create a progress bar to monitor Python code<\/a>. This post will show you how to automatically stop long-running code with the&nbsp;<strong>stopit<\/strong>&nbsp;package.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-getting-started-with-stopit\"><strong>Getting started with stopit<\/strong><\/h2>\n\n\n\n<p>To get started with&nbsp;<strong>stopit<\/strong>, you can install it via pip:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install stopit<\/code><\/pre>\n\n\n\n<p>In our first example, we\u2019ll use a context manager to stop the code we want to execute after a timeout limit is reached.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import stopit\n \nwith stopit.ThreadingTimeout(5) as context_manager:\n     \n    # sample code we want to run...\n    for i in range(10**8):\n        i = i * 2\n     \n# Did code finish running in under 5 seconds?\nif context_manager.state == context_manager.EXECUTED:\n    print(\"COMPLETE...\")\n \n# Did code timeout?\nelif context_manager.state == context_manager.TIMED_OUT:\n    print(\"DID NOT FINISH...\")<\/code><\/pre>\n\n\n\n<p>The result of running the above code will print out&nbsp;<strong>\u201cDID NOT FINISH\u2026\u201d<\/strong>. Above, we just need to specify the number of seconds we want the timeout limit to be \u2013 in this case, 5. Inside of the&nbsp;<em>with<\/em>&nbsp;statement, we specify the code we want to run with the timeout limit. For your use case, you\u2019ll just need to change the code within the&nbsp;<em>with<\/em>&nbsp;statement to whatever code you want to run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using a stopit decorator<\/strong><\/h2>\n\n\n\n<p>Another way of using&nbsp;<strong>stopit<\/strong>&nbsp;to end code execution is with the&nbsp;<em>timeoutable<\/em>&nbsp;decorator. &nbsp;If you\u2019re not familiar with decorators,&nbsp;<a href=\"https:\/\/towardsdatascience.com\/how-to-use-decorators-in-python-by-example-b398328163b\">check out this blog post<\/a>.<\/p>\n\n\n\n<p>Using the&nbsp;<em>timeoutable<\/em>&nbsp;decorator is handy when you\u2019re calling a function and want to stop its execution once a certain amount of time has been reached. The only piece of code you need to add is the decorator at the top of whatever function you want, like below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from stopit import threading_timeoutable as timeoutable\n \n@timeoutable()\ndef count():\n \n    for i in range(10**8):\n        i = i * 2\n     \n    return i<\/code><\/pre>\n\n\n\n<p>Next, when you call the above function, you just need to add a parameter specifying the number of seconds you want to use for the timeout limit. If the timeout limit is reached, None is returned.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"558\" height=\"144\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/python-stopit-timeoutable-the-automatic-net.png\" alt=\"\" class=\"wp-image-115170 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/python-stopit-timeoutable-the-automatic-net.png 558w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/python-stopit-timeoutable-the-automatic-net-300x77.png 300w\" data-sizes=\"(max-width: 558px) 100vw, 558px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 558px; aspect-ratio: 558\/144;\" \/><\/figure>\n\n\n\n<p>If your function has parameters, you just need to add those in the function call along with the timeout parameter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@timeoutable()\ndef count(upper):\n \n    for i in range(upper):\n        i = i * 2\n     \n    return i\n \nresult = count(timeout = 5, upper = 10**8)\nprint(result)<\/code><\/pre>\n\n\n\n<p>Lastly, it can be useful to know where in the function code the execution stops. There\u2019s not an inherent way of getting that with&nbsp;<strong>stopit<\/strong>, but depending on what code you\u2019re running it may be possible to find out. For example, in our function above, we can use the&nbsp;<strong>tqdm<\/strong>&nbsp;package to track the iterations in the for loop. To learn more about&nbsp;<strong>tqdm<\/strong>,&nbsp;<a href=\"https:\/\/theautomatic.net\/2020\/10\/12\/how-to-create-a-progress-bar-in-python\/\">check out this post<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from tqdm import tqdm\n \n@timeoutable()\ndef count(upper):\n \n    for i in tqdm(range(upper)):\n        i = i * 2\n     \n    return i<\/code><\/pre>\n\n\n\n<p>As you can see in the snapshot below,&nbsp;<strong>tqdm<\/strong>&nbsp;prints out a progress bar keeping tracking of the number of iterations in the for loop. Once the time limit is reached (5 seconds in this case), we can tell that we timed out after 18,464,324 iterations.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"640\" height=\"116\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/python-stopit-tqdm.png\" alt=\"\" class=\"wp-image-115176 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/python-stopit-tqdm.png 640w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/python-stopit-tqdm-300x54.png 300w\" data-sizes=\"(max-width: 640px) 100vw, 640px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 640px; aspect-ratio: 640\/116;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>That\u2019s it for now! You can check out the documentation for&nbsp;<strong>stopit<\/strong>&nbsp;by&nbsp;<a href=\"https:\/\/pypi.org\/project\/stopit\/\">clicking here<\/a>. If you want to learn more about Python, check out&nbsp;<a href=\"https:\/\/365datascience.pxf.io\/ORy4Dz\">365 Data Science by clicking here!<\/a><\/p>\n\n\n\n<p><em>Visit TheAutomatic.net for additional insight on this topic:  <a href=\"https:\/\/theautomatic.net\/2021\/11\/27\/how-to-stop-long-running-code-in-python\/\">https:\/\/theautomatic.net\/2021\/11\/27\/how-to-stop-long-running-code-in-python\/<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will show you how to automatically stop long-running code with the stopit package.<\/p>\n","protected":false},"author":388,"featured_media":81840,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338,341,352,344],"tags":[806,865,595,10778,10779],"contributors-categories":[13695],"class_list":{"0":"post-115154","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"category-quant-development","12":"category-quant-north-america","13":"category-quant-regions","14":"tag-data-science","15":"tag-github","16":"tag-python","17":"tag-python-stopit","18":"tag-tqdm-package","19":"contributors-categories-theautomatic-net"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Stop Long-running Code in Python | IBKR Quant<\/title>\n<meta name=\"description\" content=\"This post will show you how to automatically stop long-running code with the stopit package.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/115154\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Stop Long-running Code in Python | IBKR Quant Blog\" \/>\n<meta property=\"og:description\" content=\"This post will show you how to automatically stop long-running code with the stopit package.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-13T16:25:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:50:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-binary-magefying.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrew Treadway\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrew Treadway\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Andrew Treadway\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\"\n\t            },\n\t            \"headline\": \"How to Stop Long-running Code in Python\",\n\t            \"datePublished\": \"2021-12-13T16:25:40+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:50:01+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/\"\n\t            },\n\t            \"wordCount\": 502,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/python-binary-magefying.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"GitHub\",\n\t                \"Python\",\n\t                \"Python stopit\",\n\t                \"tqdm package\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"Quant North America\",\n\t                \"Quant Regions\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/\",\n\t            \"name\": \"How to Stop Long-running Code in Python | IBKR Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/python-binary-magefying.jpg\",\n\t            \"datePublished\": \"2021-12-13T16:25:40+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:50:01+00:00\",\n\t            \"description\": \"This post will show you how to automatically stop long-running code with the stopit package.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-stop-long-running-code-in-python\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/python-binary-magefying.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/python-binary-magefying.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\",\n\t            \"name\": \"Andrew Treadway\",\n\t            \"description\": \"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \\\/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\\\/\\\/www.linkedin.com\\\/in\\\/andrew-treadway-a3b19b103\\\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/theautomatic.net\\\/about-me\\\/\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/andrewtreadway\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Stop Long-running Code in Python | IBKR Quant","description":"This post will show you how to automatically stop long-running code with the stopit package.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/115154\/","og_locale":"en_US","og_type":"article","og_title":"How to Stop Long-running Code in Python | IBKR Quant Blog","og_description":"This post will show you how to automatically stop long-running code with the stopit package.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/","og_site_name":"IBKR Campus US","article_published_time":"2021-12-13T16:25:40+00:00","article_modified_time":"2022-11-21T14:50:01+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-binary-magefying.jpg","type":"image\/jpeg"}],"author":"Andrew Treadway","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Andrew Treadway","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/"},"author":{"name":"Andrew Treadway","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc"},"headline":"How to Stop Long-running Code in Python","datePublished":"2021-12-13T16:25:40+00:00","dateModified":"2022-11-21T14:50:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/"},"wordCount":502,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-binary-magefying.jpg","keywords":["Data Science","GitHub","Python","Python stopit","tqdm package"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development","Quant North America","Quant Regions"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/","name":"How to Stop Long-running Code in Python | IBKR Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-binary-magefying.jpg","datePublished":"2021-12-13T16:25:40+00:00","dateModified":"2022-11-21T14:50:01+00:00","description":"This post will show you how to automatically stop long-running code with the stopit package.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-stop-long-running-code-in-python\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-binary-magefying.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-binary-magefying.jpg","width":1000,"height":563,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc","name":"Andrew Treadway","description":"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\/\/www.linkedin.com\/in\/andrew-treadway-a3b19b103\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.","sameAs":["https:\/\/theautomatic.net\/about-me\/"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/andrewtreadway\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/python-binary-magefying.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/115154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/388"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=115154"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/115154\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/81840"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=115154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=115154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=115154"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=115154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}