{"id":26054,"date":"2018-02-08T21:49:34","date_gmt":"2018-02-08T16:19:34","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/"},"modified":"2018-02-08T21:49:34","modified_gmt":"2018-02-08T16:19:34","slug":"solved-access-global-variable-across-multiple-modules-and-threads","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/","title":{"rendered":"[Solved] Access global variable across multiple modules and threads"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-48686221\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"48686221\" data-parentid=\"48681438\" data-score=\"4\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The problem is that two instances of the variable X are created in such a set-up: one in <code>a.py<\/code> and another in <code>b.py<\/code>. The problem also occurs in this example that reduces it to the essence &#8212; it is not related to creating threads:<\/p>\n<h2>Problem in most simple form<\/h2>\n<p>File <code>a.py<\/code>:<\/p>\n<pre><code>X = \"original value\"\nimport b\nif __name__ == '__main__':\n    b.monitor()\n    print ('A', X)\n<\/code><\/pre>\n<p>File <code>b.py<\/code>:<\/p>\n<pre><code>import a\ndef monitor():\n    print ('B', a.X)\n    a.X = \"new value\"\n    print ('B', a.X)\n<\/code><\/pre>\n<p>When running <code>a.py<\/code> the output is:<\/p>\n<blockquote>\n<p>B original value<br \/>\n  B new value<br \/>\n  A original value<\/p>\n<\/blockquote>\n<p>If the same <code>X<\/code> were accessed, then the last line of the output would have read:<\/p>\n<blockquote>\n<p>A new value<\/p>\n<\/blockquote>\n<h3>Explanation<\/h3>\n<p>This happens because <code>import<\/code> creates new instances of all global names therein. It will do this only upon the first time <code>import a<\/code> is encountered, and it really is the first time here. The module created by <em>execution<\/em> of <code>a.py<\/code> is named <code>__main__<\/code>, not <code>a<\/code>. Since this name is different from <code>a<\/code>, <code>import a<\/code> will really import the module, and not just reference the already loaded <code>__main__<\/code> module. See also the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.python.org\/3\/reference\/import.html#the-module-cache\">Python documentation<\/a> for a description of how module names are resolved.<\/p>\n<p>As a consequence the names in these two modules are distinct; <code>import a<\/code> does not make the <code>__main__<\/code> names accessible to the other module.<\/p>\n<h3>Solution<\/h3>\n<p>A quick solution is to replace the <code>import a<\/code> statement in <code>b.py<\/code> with an <code>import __main__<\/code> statement, and use <code>__main__.X<\/code> as the qualified name you want to access in <code>b.py<\/code>. But this is not regarded the advised way to proceed.<\/p>\n<p>A more robust way is to ensure the global variables are not defined in the executed script, but in an imported module &#8212; one that is imported by <em>both<\/em> <code>a.py<\/code> and <code>b.py<\/code>. The second <code>import<\/code> will make the names available that were defined when the first <code>import<\/code> was parsed:<\/p>\n<p>File <code>common.py<\/code>:<\/p>\n<pre><code>X = \"original value\"\n<\/code><\/pre>\n<p>File <code>a.py<\/code>:<\/p>\n<pre><code>import common\nimport b\nif __name__ == '__main__':\n    b.monitor()\n    print ('A', common.X)\n<\/code><\/pre>\n<p>File <code>b.py<\/code>:<\/p>\n<pre><code>import common\ndef monitor():\n    print ('B', common.X)\n    common.X = \"new value\"\n    print ('B', common.X)\n<\/code><\/pre>\n<p>Now the output is:<\/p>\n<blockquote>\n<p>B original value<br \/>\n  B new value<br \/>\n  A new value<\/p>\n<\/blockquote>\n<h2>Solution applied to your code<\/h2>\n<p>File <code>common.py<\/code>:<\/p>\n<pre><code>import time\nX = {}\ndef set():\n    global X\n    X[time.time()] = 1\n\ndef get():\n    global X\n    return X\n<\/code><\/pre>\n<p>File <code>a.py<\/code>:<\/p>\n<pre><code>import common\nimport time\nimport b\n\nif __name__ == '__main__':\n    b.monitor()\n    while True:\n        time.sleep(1)\n        common.set()\n<\/code><\/pre>\n<p>File <code>b.py<\/code>:<\/p>\n<pre><code>import common\nimport threading\nimport time\n\ndef f():\n    while True:\n        time.sleep(1)\n        print common.get(), 'in b'\n\ndef monitor():\n    t = threading.Thread(target=f)\n    t.setDaemon(True)\n    t.start()\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Access global variable across multiple modules and threads <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The problem is that two instances of the variable X are created in such a set-up: one in a.py and another in b.py. The problem also occurs in this example that reduces it to the essence &#8212; it is not related to creating threads: Problem in most simple form File a.py: X = &#8220;original &#8230; <a title=\"[Solved] Access global variable across multiple modules and threads\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/\" aria-label=\"More on [Solved] Access global variable across multiple modules and threads\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[2450,884,349],"class_list":["post-26054","post","type-post","status-publish","format-standard","hentry","category-solved","tag-global-variables","tag-multithreading","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Access global variable across multiple modules and threads - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Access global variable across multiple modules and threads - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The problem is that two instances of the variable X are created in such a set-up: one in a.py and another in b.py. The problem also occurs in this example that reduces it to the essence &#8212; it is not related to creating threads: Problem in most simple form File a.py: X = &quot;original ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-08T16:19:34+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Access global variable across multiple modules and threads\",\"datePublished\":\"2018-02-08T16:19:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/\"},\"wordCount\":298,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"global-variables\",\"multithreading\",\"python\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/\",\"name\":\"[Solved] Access global variable across multiple modules and threads - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2018-02-08T16:19:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Access global variable across multiple modules and threads\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Access global variable across multiple modules and threads - JassWeb","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:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Access global variable across multiple modules and threads - JassWeb","og_description":"[ad_1] The problem is that two instances of the variable X are created in such a set-up: one in a.py and another in b.py. The problem also occurs in this example that reduces it to the essence &#8212; it is not related to creating threads: Problem in most simple form File a.py: X = \"original ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/","og_site_name":"JassWeb","article_published_time":"2018-02-08T16:19:34+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Access global variable across multiple modules and threads","datePublished":"2018-02-08T16:19:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/"},"wordCount":298,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["global-variables","multithreading","python"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/","url":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/","name":"[Solved] Access global variable across multiple modules and threads - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2018-02-08T16:19:34+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-access-global-variable-across-multiple-modules-and-threads\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Access global variable across multiple modules and threads"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=26054"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26054\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=26054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=26054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=26054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}