<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Threads on shocksolution.com</title><link>https://shocksolution.com/tags/threads/</link><description>Recent content in Threads on shocksolution.com</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 19 Aug 2008 22:50:07 +0000</lastBuildDate><atom:link href="https://shocksolution.com/tags/threads/index.xml" rel="self" type="application/rss+xml"/><item><title>Python threads are easy (with example)</title><link>https://shocksolution.com/posts/python-threads-are-easy-with-example/</link><pubDate>Tue, 19 Aug 2008 22:50:07 +0000</pubDate><guid>https://shocksolution.com/posts/python-threads-are-easy-with-example/</guid><description>&lt;p&gt;It&amp;rsquo;s remarkably easy to spawn a Python thread.  However, before doing so, I caution you that a Python thread is not the same thing as an OS thread.  Python threads run within the Python interpreter, but the Python interpreter always executes in a single process.  The reasons why have already been explained elsewhere, so I refer you to the &lt;a href="http://docs.python.org/api/threads.html" title="thread%20module%20docs"&gt;thread module documentation&lt;/a&gt; to learn about the Global Interpreter Lock.  You probably have objections to this state of affairs, and I assure you they have already been &lt;a href="http://blog.snaplogic.org/?p=94" title="Objections%20to%20the%20Global%20Interpreter%20Lock"&gt;voiced&lt;/a&gt; by Juergen Brendel and &lt;a href="http://%3C%3Cwww.artima.com/weblogs/viewpost.jsp?thread=214235&amp;amp;gt%3E;" title="Guido%20van%20Rossum's%20response"&gt;responded to&lt;/a&gt; by Guido van Rossum (creator of Python).  Anyway, the upshot is that Python can only utilize one core of a multi-core CPU.  This isn&amp;rsquo;t such a big deal for me because I&amp;rsquo;m a scientific programmer, and if I really need to write parallel code it&amp;rsquo;s going to have to run on a cluster or a grid.  Threads don&amp;rsquo;t help with that. Having said all that, threads in Python are still useful.  I will detail one example in which I spawn a thread to load a large binary file.  While this doesn&amp;rsquo;t spread the work across multiple CPU cores, it does enable the GUI to remain interactive while the file loads. All you have to do to create a Python thread is create a class that is derived from Thread. In the example below, I derived a class called Loader, which &amp;ldquo;wraps&amp;rdquo; the function that actually reads the binary files. The __init__ method accepts the filename and other options as arguments. The run() method is required. Don&amp;rsquo;t call run() directly&amp;ndash;instead, call the start() method (inherited from the base class) to start the thread.&lt;/p&gt;</description></item><item><title>Fun with threads in Python and wxPython</title><link>https://shocksolution.com/posts/fun-with-threads-in-python-and-wxpython/</link><pubDate>Mon, 11 Aug 2008 22:12:07 +0000</pubDate><guid>https://shocksolution.com/posts/fun-with-threads-in-python-and-wxpython/</guid><description>&lt;p&gt;I have finally gotten back to programming in the last couple of days.  Our project has finally started to generate a lot of data, so I&amp;rsquo;ve been refactoring and improving my code that &lt;a href="https://shocksolution.com/blog/2008/06/25/reading-labview-binary-files-with-python/" title="Reading%20Labview%20binaries%20with%20Python"&gt;reads data stored in LabView binaries&lt;/a&gt;.  Today I spent a lot of time creating a GUI for browsing data.  Arguably, this wasn&amp;rsquo;t the best use of my time, but I learned a lot about multi-threaded Python GUI programming with wxPython.  You can find a gold mine of &lt;a href="http://wiki.wxpython.org/LongRunningTasks" title="Multi-threading%20with%20wxpython"&gt;information on the multi-threaded wx programming&lt;/a&gt; at the wxPython wiki.  Because the LabView binary data has to be read sequentially, and the files are rather large, it takes a long time to read in a file.  I spawn a thread to handle the file reading,  while allowing the GUI to remain responsive.  The thread posts messages to the GUI window, which are used to update the user on the status of the file reading operation.  When the file is read, a final message containing the data is posted to the window.  It&amp;rsquo;s really pretty slick now that I&amp;rsquo;ve figured out how to do it. I will soon post a clever scheme to capture text output from the file-reading function, and display it in the GUI, without making substantial changes to the file-reading function.&lt;/p&gt;</description></item></channel></rss>