Archive for February, 2010

Note: Wordpress likes to convert my < > inside [ code ] blocks to to &amp;gt; and &amp;lt; so sorry about the formatting

After Beazley’s talk at PyCon "Understanding the Python GIL" I released I had never done any work that released the GIL, spawned threads, did some work, and then restored the GIL. So I wanted to see if I could so something like that with Boost::Python and Boost::Thread and the type of performance I’d get from it with an empty while loop as the baseline. So I hacked up some quick and dirty C++ code and quick bit of runable Python to test out the resulting module and away I went. Below are the code snippets, links to bitbucket, and the results of the Python runable.

#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/python.hpp>

class ScopedGILRelease {
public:
	inline ScopedGILRelease() { m_thread_state = PyEval_SaveThread(); }
	inline ~ScopedGILRelease() { PyEval_RestoreThread(m_thread_state); m_thread_state = NULL; }
private:
	PyThreadState* m_thread_state;
};

void loop(long count)
{
	while (count != 0) {
		count -= 1;
	}
	return;
}

void nogil(int threads, long count)
{
	if (threads <= 0 || count <= 0)
		return;

	ScopedGILRelease release_gil = ScopedGILRelease();
	long thread_count = (long)ceil(count / threads);

	std::vector<boost::shared_ptr<boost::thread> > v_threads;
	for (int i=0; i != threads; i++) {
		boost::shared_ptr<boost::thread> m_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(loop,thread_count)));
		v_threads.push_back(m_thread);
	}

	for (int i=0; i != v_threads.size(); i++)
		v_threads[i]->join();

	return;
}

BOOST_PYTHON_MODULE(nogil)
{
	using namespace boost::python;
	def("nogil", nogil);
}

Then I used the following Python script to run some quick tests.

import time
import nogil

def timer(func):
	def wrapper(*arg):
		t1 = time.time()
		func(*arg)
		t2 = time.time()
		print "%s took %0.3f ms" % (func.func_name, (t2-t1)*1000.0)
	return wrapper

@timer
def loopone():
	count = 5000000
	while count != 0:
		count -= 1

@timer
def looptwo():
	count = 5000000
	nogil.nogil(1,count)

@timer
def loopthree():
	count = 5000000
	nogil.nogil(2,count)

@timer
def loopfour():
	count = 5000000
	nogil.nogil(4,count)

@timer
def loopfive():
	count = 5000000
	nogil.nogil(6,count)

def main():
	loopone()
	looptwo()
	loopthree()
	loopfour()
	loopfive()

if __name__ == '__main__':
	main()

The results I got were quite interesting and very consistent on my MacBook Pro. I ran the script about 1,000 times and got roughly the same results every time.

loopone took 364.159 ms (pure python)
looptwo took 15.295 ms (c++, no GIL, single thread)
loopthree took 7.763 ms (c++, no GIL, two threads)
loopfour took 8.119 ms (c++, no GIL, four threads)
loopfive took 11.102 ms (c++, no GIL, six threads)

Anyway, that’s all really. Nothing profound here, no super insightful ending. Just hey look and stuff is faster and I might use this. All the code for this is available in my bitbucket repo. http://bitbucket.org/wwitzel3/code/src/tip/nogil/

You will require Boost Library including Boost Python and Boost Thread as well as Python libraries and includes to build this. For boost, bjam –with-python –with-thread variant=release toolset=gcc is all I did on my Mac. Then I added the resulting lib’s as Framework dependencies in Xcode along with the Python.framework.

PyCon 2010 this year for me has come and gone. This year was just as good as ever.

My wife and I drove up from Florida this year. It was a 5-hour drive and was actually pretty pleasant, considering she drove and I slept for initial and the return trip. I’m pretty much a horrible person to spend more than 2-hours in a car with anyway so it is much better if I sleep the whole time.

The location in Downtown Atlanta was great. Lots of bars, restaurants, and nightlife to choose from when the con events started to die down. Though the fact there was a liquor store across the street from the Hyatt pretty much meant that the con never died down. Though I had friends in Chicago who took us for nights on the town and out and about in the city, this location was much better overall for the rest of the attendees if you ask me.

The best place if you ask me was this 24/7 diner called the Landmark Diner. Great American style diner food along side some greek classics. It was tops on my list for breakfast in the morning and for that after hours drunken feast. In my 5-days in Atlanta we went here 4 times. I highly recommend it if you are ever in the city. It is on the corner of Forsyth and Luckie in Downtown.

The talks I went to were good. The open spaces I attended were good. The sprints I attended were good. Not much more to say really. You can find plenty of people who are more picky about the specifics than me so I’ll leave it at that. I had a great time all around with con. Everything was well organized and the efforts of the committee, and all the volunteers was impressive. I volunteered this year as Session Chair for some talks so if you go watch some videos you’ll see me introducing some people.

I met a ton of people and had a lot of hallway talks and late night talks about everything ranging from Python to politics. It is always great fun and I will repeat what many others have said before. Don’t be shy and don’t be afraid to skip and an official talk and join a hallway talk instead, they can be great. Oh also, I am stealing Jack Diederich’s (first heard from) idea for the Lunch Session next PyCon. Find a table with no one you know and sit at it. I love that idea.

I had some very late nights and some excellent times and I met a ton of great people. I’d like to thank some people in no particular order. CCP guys, Chicago Python guys, Jack Diederich, Jakub Vysoky, Travis Cline, Michael Trier

Looking forward to next year!

I had a lot of fun attending last year and I didn’t want to miss this year, specially considering I’m exploring my options with work at them moment and I’ve been told PyCon is a great place for that.

So I officially took care of everything I needed to today. I’ll be attending PyCon in Atlanta this year from the 19th thru the 23rd. I’ll be staying at the Ellis, mainly because it was a $500 savings over staying at the Hyatt Regency (being a AAA member paid off finally) and it is only a 2 city block walk to the main venue.

I’ll be attending 2-days of the sprints this year and I’m really looking forward those. Living so close in Florida I’ll be driving up the 5 hours to Atlanta instead of flying.

If anyone else is staying at the Ellis drop me an email we can breakfast together before our morning walk to the Hyatt.