<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erics Tech Blog &#187; python</title>
	<atom:link href="http://eric.lubow.org/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://eric.lubow.org</link>
	<description>Thoughts, musings, and other idealistic (sometimes useful) systems and development hoopla.</description>
	<lastBuildDate>Fri, 18 Nov 2011 14:56:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>MySQL for Python</title>
		<link>http://eric.lubow.org/2010/book-reviews/mysql-for-python/</link>
		<comments>http://eric.lubow.org/2010/book-reviews/mysql-for-python/#comments</comments>
		<pubDate>Mon, 27 Dec 2010 07:10:22 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Book Reviews]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=812</guid>
		<description><![CDATA[I am always for using the right tool for the right job. A lot of time, that tool is Python. I have always had trouble finding solid documentation on using MySQL with Python. There was generally enough to get by, but the more the merrier. Enter MySQL for Python by Albert Lukaszewski. As I mention [...]]]></description>
			<content:encoded><![CDATA[<p>I am always for using the right tool for the right job.  A lot of time, that tool is Python.  I have always had trouble finding solid documentation on using MySQL with Python.  There was generally enough to get by, but the more the merrier.  Enter MySQL for Python by Albert Lukaszewski. <span id="more-812"></span> <div id="attachment_813" class="wp-caption alignright" style="width: 110px"><a href="http://eric.lubow.org/wp-content/uploads/2010/12/mysql_for_python.jpg"><img src="http://eric.lubow.org/wp-content/uploads/2010/12/mysql_for_python.jpg" alt="MySQL for Python Cover" title="MySQL for Python Cover" width="100" height="129" class="size-full wp-image-813" /></a><p class="wp-caption-text">MySQL for Python</p></div></p>
<p>As I mention in most of my reviews, on the of the things I love about Packt Publishing books is that they typically follow the same pattern: installation, teaching, project, summary.  And my favorite piece is the mini projects given at the end of each chapter.  It&#8217;s a little reality check for the reader reminding them that everything that they are doing has a real life application.</p>
<p>So skipping the installation chapter and jumping right in to the teaching, there is a lot of discussion about how to do the common stuff that one does in MySQL through Python.  If you have a lot of experience with MySQL, then this is a handy reference.  I also really like the fact that scattered throughout the book is information on becoming a better programmer (like tradeoffs on memory efficiency vs. performance).</p>
<p>Chapter 4 contained information about exception handling.  Just like previous chapters, the amount of information on the handling of warnings and exceptions exceeds the boundaries of just MySQL in Python.  This was a great review for any Python programmer.  No need for further discussion as this chapter is available from Packt Publishing <a href="https://www.packtpub.com/sites/default/files/0189OS-Chapter-4-Exception-Handling.pdf">here</a>.</p>
<p>The next few chapters deal with data manipulation in Python.  This includes INSERTs, UPDATEs, DELETEs, etc.  Chapter 8 specifically deals with user management in MySQL.  This is a great skill to have regardless of the interface that you are using to tie into MySQL.  Most programmers neglect the concept of administration and leave it to the DBAs or the SysAdmins. Albert takes the reader through some admin exercises including user management, backup and recovery, and accessing the MySQL meta information like <em>information_schema</em> tables.  There was also discussion about the various storage engines which was unexpected.</p>
<p>Bouncing backwards a little was a lesson on the string and aggregation functions built into MySQL.  A lot of these capabilities are usually handled programatically and not off-loaded to the database like it should be.  Again, this is a great refresher (or even initial) lesson for any programmer who spends a lot of time building and maintaining complex queries.</p>
<p>Overall this book was another great teaching tool put forth by the folks at Packt.  The audience is definitely not at the beginner Python programmer, but an intermediate level developer would have no issues understanding everything.  You can find this book either at <a href="http://www.amazon.com/MySQL-Python-Albert-Lukaszewski/dp/1849510180">Amazon</a> or directly from <a href="https://www.packtpub.com/mysql-for-python-database-access-made-easy/book">Packt Publishing</a>.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/book-reviews/mod-security-2-5-by-magnus-mischel/' rel='bookmark' title='Mod-Security 2.5 by Magnus Mischel'>Mod-Security 2.5 by Magnus Mischel</a></li>
<li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2007/book-reviews/building-telephony-systems-with-asterisk/' rel='bookmark' title='Building Telephony Systems With Asterisk'>Building Telephony Systems With Asterisk</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/book-reviews/mysql-for-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Philosophical Python</title>
		<link>http://eric.lubow.org/2010/python/philosophical-python/</link>
		<comments>http://eric.lubow.org/2010/python/philosophical-python/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 13:30:43 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[Poetry]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=552</guid>
		<description><![CDATA[Having spent a lot of time in the Perl world and even trying to write a Perl poem here and there, I decided to give some Python poetry a try. It turned out to be a bit more philosophical than poetic. But it compiles and that&#8217;s what counts; even though it doesn&#8217;t do anything useful [...]]]></description>
			<content:encoded><![CDATA[<p>Having spent a lot of time in the Perl world and even trying to write a <a href="http://eric.lubow.org/category/perl/poetry/">Perl poem</a> here and there, I decided to give some Python poetry a try.  It turned out to be a bit more philosophical than poetic. But it compiles and that&#8217;s what counts; even though it doesn&#8217;t do anything useful (or anything at all).<br />
<span id="more-552"></span><br />
Since as people, we get the most out of something by filling in small spaces with our imagination (think of how comic books are so successful), fill in the words that might be missing.  Either way, enjoy.  Hope you get as much from reading it as I did from writing it.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> people<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">class</span> Me<span style="color: black;">&#40;</span>being<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">mind</span> <span style="color: #66cc66;">=</span> MyThoughts<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">body</span> <span style="color: #66cc66;">=</span> MyBeing<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">soul</span> <span style="color: #66cc66;">=</span> MyEssence<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> inner<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers <span style="color: #66cc66;">=</span> <span style="color: #008000;">None</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mind_answers <span style="color: #66cc66;">=</span> <span style="color: #008000;">self</span>.<span style="color: black;">mind</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body_answers <span style="color: #66cc66;">=</span> <span style="color: #008000;">self</span>.<span style="color: black;">body</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; soul_answers <span style="color: #66cc66;">=</span> <span style="color: #008000;">self</span>.<span style="color: black;">soul</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers <span style="color: #66cc66;">=</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>mind_answers<span style="color: #66cc66;">,</span> body_answers<span style="color: #66cc66;">,</span> soul_answers<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> MeaningOfLife <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> answers:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> Awareness<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;Searching for meaning&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers <span style="color: #66cc66;">=</span> <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>Intelligence<span style="color: #66cc66;">,</span> mind_answers<span style="color: #66cc66;">,</span> body_answers<span style="color: #66cc66;">,</span> soul_answers<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> answers<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span> Awareness<span style="color: #66cc66;">,</span> MeaningOfLife:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> MeaningOfLife</div></div>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/' rel='bookmark' title='When To Use MySQL Cursor Classes In Python'>When To Use MySQL Cursor Classes In Python</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/python/philosophical-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When To Use MySQL Cursor Classes In Python</title>
		<link>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/</link>
		<comments>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 15:00:40 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=526</guid>
		<description><![CDATA[I have been writing a lot of code that has been interacting with MySQL lately. Sometimes I find it easier to work the result set in a dictionary form and other times it is easier with an array. But in order to not break all your code, it is necessary to set a default cursor [...]]]></description>
			<content:encoded><![CDATA[<p>I have been writing a lot of code that has been interacting with MySQL lately.  Sometimes I find it easier to work the result set in a dictionary form and other times it is easier with an array.  But in order to not break all your code, it is necessary to set a default cursor class that keeps your code consistent.  More often than not, I find using using a arrays is easier since I just want quick access to all the retrieved data.  I also end up making my SELECT calls while specifying the columns and order of the columns I want returned.</p>
<p>The reason that using cursor classes is handy is because Python doesn&#8217;t come with a <strong>mysql_fetch_assoc</strong> like PHP or <strong>selectrow_hashref</strong> like Perl&#8217;s DBI interface.  Python uses cursor dictionaries to bridge this gap.  Ultimately your result is the same.  But as with Perl and PHP, defaulting to cursor dictionaries isn&#8217;t a good idea for larger datasets because of the extra processing time and memory required to convert the data.<br />
<span id="more-526"></span></p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<br />
conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'localhost'</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">'user'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; passwd<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'secret'</span><span style="color: #66cc66;">,</span> db<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'test'</span><span style="color: black;">&#41;</span><br />
cursor <span style="color: #66cc66;">=</span> conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT this,that FROM foobar&quot;</span><span style="color: black;">&#41;</span><br />
rows <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>This then allows me to do just call the DictCursor (dictionary based cursor) on a single cursor object for a particular query and leave all the rest of the queries as array based results.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb.<span style="color: black;">cursors</span><br />
<br />
conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'localhost'</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">'user'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; passwd<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'secret'</span><span style="color: #66cc66;">,</span> db<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'test'</span><span style="color: black;">&#41;</span><br />
cursor <span style="color: #66cc66;">=</span> conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span>cursorclass<span style="color: #66cc66;">=</span>MySQLdb.<span style="color: black;">cursors</span>.<span style="color: black;">DictCursor</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT this,that FROM foobar&quot;</span><span style="color: black;">&#41;</span><br />
rows <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> rows:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> row<span style="color: black;">&#91;</span><span style="color: #483d8b;">'this'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> row<span style="color: black;">&#91;</span><span style="color: #483d8b;">'that'</span><span style="color: black;">&#93;</span><br />
cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p><strong>Note:</strong> Doing something like changing the connection string to use a default cursor class of dictionary cursor in an existing code base is not a good idea.  It will make your results come back as dictionaries when your current code expects arrays.  Yes, I did this and had to track it down the hard way. Hopefully this saves you some trouble.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'localhost'</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">'user'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; passwd<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'secret'</span><span style="color: #66cc66;">,</span> db<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'test'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; cursorclass<span style="color: #66cc66;">=</span>MySQLdb.<span style="color: black;">cursors</span>.<span style="color: black;">DictCursor</span><span style="color: black;">&#41;</span></div></div>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/' rel='bookmark' title='Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync'>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/database-readwrite-splitting-in-frameworksorms/' rel='bookmark' title='Database Read/Write Splitting in Frameworks/ORMs'>Database Read/Write Splitting in Frameworks/ORMs</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python Multiprocessing Pools and MySQL</title>
		<link>http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/</link>
		<comments>http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 12:00:00 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[multprocessing]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[pool]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=486</guid>
		<description><![CDATA[There really isn&#8217;t a solid Python module for multiprocessing and MySQL. Now this may be because MySQL on a single server is disk bound and therefore limited in speed or just because no one has written it. So here is a quick and dirty example using the Pool module in multiprocessing in Python 2.6 and [...]]]></description>
			<content:encoded><![CDATA[<p>There really isn&#8217;t a solid Python module for multiprocessing and MySQL.  Now this may be because MySQL on a single server is disk bound and therefore limited in speed or just because no one has written it.  So here is a quick and dirty example using the <strong>Pool</strong> module in <em>multiprocessing</em> in Python 2.6 and <a href="http://mysql-python.sourceforge.net/MySQLdb.html">MySQLdb</a>.</p>
<p>I also tried using <a href="http://code.google.com/p/pysqlpool/">PySQLPool</a>.  This was designed for threading and not forking as I am doing with Pool method.  Although I am sure it is possible to use PySQLPool with forking by passing the connection (pool) object down to the child process or possibly doing something with IPC, I decided to keep it simple (although slightly more expensive) and instantiate MySQLdb connections upon fork.<br />
<span id="more-486"></span></p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<span style="color: #ff7700;font-weight:bold;">import</span> multiprocessing<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; tablename <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'address_demographics'</span><br />
&nbsp; &nbsp; inserts <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">id</span> <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span> <br />
&nbsp; &nbsp; max_id <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1000000</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">id</span> <span style="color: #66cc66;">&lt;</span> max_id:<br />
&nbsp; &nbsp; &nbsp; &nbsp; sql <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;&quot;&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; INSERT IGNORE INTO `%s` (person_id)<br />
&nbsp; &nbsp; &nbsp; &nbsp; SELECT person_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM `people`<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE id BETWEEN %d AND %d<br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span> % <span style="color: black;">&#40;</span>tablename<span style="color: #66cc66;">,</span><span style="color: #008000;">id</span><span style="color: #66cc66;">,</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span> + <span style="color: #ff4500;">100000</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; inserts.<span style="color: black;">append</span><span style="color: black;">&#40;</span>sql<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">id</span> +<span style="color: #66cc66;">=</span> <span style="color: #ff4500;">100001</span><br />
&nbsp; &nbsp; pool <span style="color: #66cc66;">=</span> multiprocessing.<span style="color: black;">Pool</span><span style="color: black;">&#40;</span>multiprocessing.<span style="color: black;">cpu_count</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pool.<span style="color: #008000;">map</span><span style="color: black;">&#40;</span>sqlWorkerInsert<span style="color: #66cc66;">,</span> inserts<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pool.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pool.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> sqlWorkerInsert<span style="color: black;">&#40;</span>sql<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;127.0.0.1&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #dc143c;">user</span> <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;USER&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;passwd <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;PASS&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;DB&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor <span style="color: #66cc66;">=</span> conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Need to do this since AUTOCOMMIT = 0 by default (wtf?)</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SET AUTOCOMMIT=1&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>sql<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>It may not be the cleanest method to open a new connection to MySQL for every worker subprocess.  Since MySQL opens a new thread for every connection, I don&#8217;t think this is that big of a deal.  However I am always open to new ways of doing things.</code></p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/' rel='bookmark' title='When To Use MySQL Cursor Classes In Python'>When To Use MySQL Cursor Classes In Python</a></li>
<li><a href='http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/' rel='bookmark' title='Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync'>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/database-readwrite-splitting-in-frameworksorms/' rel='bookmark' title='Database Read/Write Splitting in Frameworks/ORMs'>Database Read/Write Splitting in Frameworks/ORMs</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</title>
		<link>http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/</link>
		<comments>http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 13:00:36 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=480</guid>
		<description><![CDATA[While writing a simple Python script to access and process data in a database, I came across an error that said: Error 2014: Commands out of sync; you can't run this command now After quite a bit of Googling and with very little findings, I had to dive in a little and try to figure [...]]]></description>
			<content:encoded><![CDATA[<p>While writing a simple Python script to access and process data in a database, I came across an error that said:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Error <span style="color: #ff4500;">2014</span>: Commands out of sync<span style="color: #66cc66;">;</span> you can<span style="color: #483d8b;">'t run this command now</span></div></div>
<p>After quite a bit of Googling and with very little findings, I had to dive in a little and try to figure out what was going on.  The whole error looked like this:<br />
<span id="more-480"></span></p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: black;">&#91;</span>elubow<span style="color: #66cc66;">@</span>web7 scripts<span style="color: black;">&#93;</span>$ ./<span style="color: #ff4500;">2014</span>_test.<span style="color: black;">py</span><br />
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:<br />
&nbsp; File <span style="color: #483d8b;">&quot;build/bdist.linux-x86_64/egg/MySQLdb/cursors.py&quot;</span><span style="color: #66cc66;">,</span> line <span style="color: #ff4500;">173</span><span style="color: #66cc66;">,</span> <span style="color: #ff7700;font-weight:bold;">in</span> execute<br />
&nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">errorhandler</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> exc<span style="color: #66cc66;">,</span> value<span style="color: black;">&#41;</span><br />
&nbsp; File <span style="color: #483d8b;">&quot;build/bdist.linux-x86_64/egg/MySQLdb/connections.py&quot;</span><span style="color: #66cc66;">,</span> line <span style="color: #ff4500;">36</span><span style="color: #66cc66;">,</span> <span style="color: #ff7700;font-weight:bold;">in</span> defaulterrorhandler<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> errorclass<span style="color: #66cc66;">,</span> errorvalue<br />
ProgrammingError: <span style="color: black;">&#40;</span><span style="color: #ff4500;">2014</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;Commands out of sync; you can't run this command now&quot;</span><span style="color: black;">&#41;</span><br />
Error <span style="color: #ff4500;">2014</span>: Commands out of sync<span style="color: #66cc66;">;</span> you can<span style="color: #483d8b;">'t run this command now<br />
Exception _mysql_exceptions.ProgrammingError: (1064, &quot;You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '</span>test_table<span style="color: #483d8b;">' at line 1&quot;) in<br />
&lt;bound method Cursor.__del__ of &lt;MySQLdb.cursors.Cursor object at 0x2aac002f9d90&gt;&gt; ignored<br />
&lt;/bound&gt;</span></div></div>
<p>The issue turned out to be a very simple one to fix.  Apparently MySQLdb doesn&#8217;t support compound MySQL statements.  That is to say that this won&#8217;t work:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SELECT COUNT(*) FROM people; SHOW TABLES LIKE addresses;'</span><span style="color: black;">&#41;</span></div></div>
<p>But this will work:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT COUNT(*) FROM people&quot;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor <span style="color: #66cc66;">=</span> conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SHOW TABLES LIKE addresses&quot;</span><span style="color: black;">&#41;</span></div></div>
<p>Make sure you break up your SQL statements into separate execute statements and you should avoid this MySQL 2014 error.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/' rel='bookmark' title='When To Use MySQL Cursor Classes In Python'>When To Use MySQL Cursor Classes In Python</a></li>
<li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/mysql-error-1033-incorrect-information-in-file/' rel='bookmark' title='MySQL Error 1033: Incorrect Information in File'>MySQL Error 1033: Incorrect Information in File</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SSH Over The Web With Web Shell</title>
		<link>http://eric.lubow.org/2009/system-administration/ssh-over-the-web-with-web-shell/</link>
		<comments>http://eric.lubow.org/2009/system-administration/ssh-over-the-web-with-web-shell/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 11:45:46 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=431</guid>
		<description><![CDATA[After reading a Tweet from Matt Cutts about being able to SSH from the iPhone (and the web in general), I had to give it a try. I am always looking for better ways to be able to check on systems when necessary. I have iPhone apps for SSHing around if I need as well, [...]]]></description>
			<content:encoded><![CDATA[<p>After reading a <a href="http://twitter.com/mattcutts/status/5952475317">Tweet</a> from <a href="http://www.mattcutts.com/">Matt Cutts</a> about being able to SSH from the iPhone (and the web in general), I had to give it a try.  I am always looking for better ways to be able to check on systems when necessary.  I have iPhone apps for SSHing around if I need as well, but like with any &#8220;new&#8221; tool, I have to try it out to see if it serves a purpose or makes my admin life easier in any way.</p>
<p>First go check out the Google Code repository for <a href="http://code.google.com/p/web-shell/">Web Shell</a>.  Webshell is written in Python and is based on Ajaxterm.  All that&#8217;s required is SSL And Python 2.3 or greater.  It works on any browser that has Javascript and can make use of AJAX.</p>
<p>The way Web Shell works is you start it up on a server and then can use a web browser to access only that machine over SSH.  The works best if you have a gateway server to a network and use a single point of entry to access the rest of the servers.  Web Shell runs on HTTPS on port 8022.  Reading the README will lead you through the same set of instructions I used below.  Once installed, we connect by using a web browser: <strong>https://server.com:8022/</strong><br />
<span id="more-431"></span></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">elubow@server:/home/web/webshell.lubow.org-8022$ ./make_certificate.sh <br />
Generating a 1024 bit RSA private key<br />
...............................................................++++++<br />
...........................................++++++<br />
writing new private key to 'webshell.pem'<br />
-----<br />
elubow@server:/home/web/webshell.lubow.org-8022$ ./webshell.py <br />
The python SSL extensions seem to be not installed.<br />
You can run WebShell without SSL encryption with the --ssl-disable command line switch.</div></div>
<p>You&#8217;ll notice that while it is possible to run the terminal without SSL, it is just not in our best interest.  So let&#8217;s go ahead and install it.  I use <a href="http://debian.org/">Debian</a> on my servers.  So in my case, it&#8217;s as simple as:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">server:~# apt-get install python-pyopenssl python-openssl<br />
Get:1 http://ftp.de.debian.org lenny/main python-openssl 0.7-2 [88.9kB]<br />
Get:2 http://ftp.de.debian.org lenny/main python-pyopenssl 0.7-2 [9.0kB]<br />
Unpacking python-openssl (from .../python-openssl_0.7-2_i386.deb) ...<br />
Setting up python-openssl (0.7-2) ...<br />
Setting up python-pyopenssl (0.7-2) ...<br />
Processing triggers for python-support ...</div></div>
<p>Then we run the server:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">elubow@server:/home/web/ssh.lubow.org-8022$ ./webshell.py <br />
WebShell (https) at 0.0.0.0, port 8022</div></div>
<p>I&#8217;ve included a screenshot from my iPhone below.  The background is interchangeable with any other transparent background image.  If you&#8217;re in need of a quick SSH or someone else&#8217;s smart phone has service and yours doesn&#8217;t with your fancy SSH programs, then this is definitely a cool and viable alternative.</p>
<table align="center" cellpadding="10px">
<tr>
<td><div id="attachment_435" class="wp-caption aligncenter" style="width: 160px"><a href="http://eric.lubow.org/wp-content/uploads/2009/11/IMG_0137.PNG"><img src="http://eric.lubow.org/wp-content/uploads/2009/11/IMG_0137-150x150.PNG" alt="Web Shell SSH on iPhone" title="Web Shell SSH" width="150" height="150" class="size-thumbnail wp-image-435" /></a><p class="wp-caption-text">Web Shell SSH on iPhone</p></div></td>
<td><div id="attachment_437" class="wp-caption aligncenter" style="width: 160px"><a href="http://eric.lubow.org/wp-content/uploads/2009/11/IMG_0138.PNG"><img src="http://eric.lubow.org/wp-content/uploads/2009/11/IMG_0138-150x150.PNG" alt="Web Shell SSH with keyboard out" title="Web Shell SSH with keyboard out" width="150" height="150" class="size-thumbnail wp-image-437" /></a><p class="wp-caption-text">Web Shell SSH with keyboard out</p></div></td>
</tr>
</table>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/book-reviews/mysql-for-python/' rel='bookmark' title='MySQL for Python'>MySQL for Python</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/system-administration/ssh-over-the-web-with-web-shell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

