{"body": "<h1>Source code for iCallSV.launchThreads</h1><div class=\"highlight\"><pre>\n<span></span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">Created on December 21, 2015</span>\n<span class=\"sd\">Description: This module will be launching functions as threads</span>\n<span class=\"sd\">@author: Ronak H Shah</span>\n<span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">::Inputs::</span>\n<span class=\"sd\">threadID: Id for the thread</span>\n<span class=\"sd\">name: name for the thread</span>\n<span class=\"sd\">counter: a counter to keep track of the thread</span>\n<span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">Taken from:</span>\n<span class=\"sd\">http://www.tutorialspoint.com/python/python_multithreading.htm</span>\n\n<span class=\"sd\">&quot;&quot;&quot;</span>\n\n<span class=\"kn\">import</span> <span class=\"nn\">logging</span>\n<span class=\"kn\">import</span> <span class=\"nn\">threading</span>\n<span class=\"kn\">import</span> <span class=\"nn\">time</span>\n\n<span class=\"n\">logger</span> <span class=\"o\">=</span> <span class=\"n\">logging</span><span class=\"o\">.</span><span class=\"n\">getLogger</span><span class=\"p\">(</span><span class=\"n\">__name__</span><span class=\"p\">)</span>\n\n<div class=\"viewcode-block\" id=\"myThread\"><a class=\"viewcode-back\" href=\"../../../iCallSV/#iCallSV.launchThreads.myThread\">[docs]</a><span class=\"k\">class</span> <span class=\"nc\">myThread</span> <span class=\"p\">(</span><span class=\"n\">threading</span><span class=\"o\">.</span><span class=\"n\">Thread</span><span class=\"p\">):</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">threadID</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"n\">counter</span><span class=\"p\">):</span>\n        <span class=\"n\">threading</span><span class=\"o\">.</span><span class=\"n\">Thread</span><span class=\"o\">.</span><span class=\"n\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">threadID</span> <span class=\"o\">=</span> <span class=\"n\">threadID</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"n\">name</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">counter</span> <span class=\"o\">=</span> <span class=\"n\">counter</span>\n\n<div class=\"viewcode-block\" id=\"myThread.run\"><a class=\"viewcode-back\" href=\"../../../iCallSV/#iCallSV.launchThreads.myThread.run\">[docs]</a>    <span class=\"k\">def</span> <span class=\"nf\">run</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        \n        <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span><span class=\"s2\">&quot;Starting </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">name</span><span class=\"p\">)</span>\n        <span class=\"c1\"># Get lock to synchronize threads</span>\n        <span class=\"n\">threadLock</span><span class=\"o\">.</span><span class=\"n\">acquire</span><span class=\"p\">()</span>\n        <span class=\"n\">print_time</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">counter</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">)</span>\n        <span class=\"c1\"># Free lock to release next thread</span>\n        <span class=\"n\">threadLock</span><span class=\"o\">.</span><span class=\"n\">release</span><span class=\"p\">()</span></div></div>\n\n\n<div class=\"viewcode-block\" id=\"print_time\"><a class=\"viewcode-back\" href=\"../../../iCallSV/#iCallSV.launchThreads.print_time\">[docs]</a><span class=\"k\">def</span> <span class=\"nf\">print_time</span><span class=\"p\">(</span><span class=\"n\">threadName</span><span class=\"p\">,</span> <span class=\"n\">delay</span><span class=\"p\">,</span> <span class=\"n\">counter</span><span class=\"p\">):</span>\n    <span class=\"k\">while</span> <span class=\"n\">counter</span><span class=\"p\">:</span>\n        <span class=\"n\">time</span><span class=\"o\">.</span><span class=\"n\">sleep</span><span class=\"p\">(</span><span class=\"n\">delay</span><span class=\"p\">)</span>\n        <span class=\"nb\">print</span> <span class=\"s2\">&quot;</span><span class=\"si\">%s</span><span class=\"s2\">: </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span> <span class=\"o\">%</span> <span class=\"p\">(</span><span class=\"n\">threadName</span><span class=\"p\">,</span> <span class=\"n\">time</span><span class=\"o\">.</span><span class=\"n\">ctime</span><span class=\"p\">(</span><span class=\"n\">time</span><span class=\"o\">.</span><span class=\"n\">time</span><span class=\"p\">()))</span>\n        <span class=\"n\">counter</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span></div>\n\n<span class=\"n\">threadLock</span> <span class=\"o\">=</span> <span class=\"n\">threading</span><span class=\"o\">.</span><span class=\"n\">Lock</span><span class=\"p\">()</span>\n<span class=\"n\">threads</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n\n<span class=\"c1\"># Create new threads</span>\n<span class=\"n\">thread1</span> <span class=\"o\">=</span> <span class=\"n\">myThread</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Thread-1&quot;</span><span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"n\">thread2</span> <span class=\"o\">=</span> <span class=\"n\">myThread</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Thread-2&quot;</span><span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">)</span>\n\n<span class=\"c1\"># Start new Threads</span>\n<span class=\"n\">thread1</span><span class=\"o\">.</span><span class=\"n\">start</span><span class=\"p\">()</span>\n<span class=\"n\">thread2</span><span class=\"o\">.</span><span class=\"n\">start</span><span class=\"p\">()</span>\n\n<span class=\"c1\"># Add threads to thread list</span>\n<span class=\"n\">threads</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">thread1</span><span class=\"p\">)</span>\n<span class=\"n\">threads</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">thread2</span><span class=\"p\">)</span>\n\n<span class=\"c1\"># Wait for all threads to complete</span>\n<span class=\"k\">for</span> <span class=\"n\">t</span> <span class=\"ow\">in</span> <span class=\"n\">threads</span><span class=\"p\">:</span>\n    <span class=\"n\">t</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">()</span>\n<span class=\"nb\">print</span> <span class=\"s2\">&quot;Exiting Main Thread&quot;</span>\n</pre></div>", "title": "iCallSV.launchThreads", "customsidebar": null, "current_page_name": "_modules/iCallSV/launchThreads", "sidebars": null, "parents": [{"link": "../../", "title": "Module code"}], "alabaster_version": "0.7.9"}