Archive for March, 2009

apache with a segmentation fault

Wednesday, March 18th, 2009
i deployed a python app on a live server and the only output i got was a blank page. there was no error in the virtual hosts log file and it stopped somewhere during the execution of the script. in the apache error log file i found the following line: child pid 15136 exit signal Segmentation fault (11) a very informative and helpful message. after a bit of googling i found out that the gdb would help. gdb is short for GNU Project Debugger. in the file /usr/share/doc/apache2.2-common/README.backtrace there is a short howto to get a stacktrace of a segmentation fault in apache… at least on debian based systems. here is a short overview of what there is to do. at first it’s necessary to install the following packages: apt-get install apache2-dbg libapr1-dbg libaprutil1-dbg gdb add the line CoreDumpDirectory /var/cache/apache2 to your apache config, usually /etc/apache/apache2.conf. after a restart of apache it should now create a memory dump named /var/cache/apache2/core which can be analysed with gdb. it might be necessary to set the maximum size of the coredump like following (inclusive restart of apache): /etc/init.d/apache2 stop ulimit -c unlimited /etc/init.d/apache2 start to analyze the core dump you need to execute the gdb like following: gdb /usr/sbin/apache2 /var/cache/apache2/core (gdb) bt full ... (gdb) quit if you use the threaded mpm (unlikely) then you need to use gdb /usr/sbin/apache2 /var/cache/apache2/core (gdb) thread apply all bt full ... (gdb) quit my dump produced following output: #0 0xb7dd86a5 in free () from /lib/libc.so.6 #1 0xb6675011 in RelinquishMagickMemory () from /usr/lib/libMagick.so.9 #2 0xb6625ba0 in DestroyDrawInfo () from /usr/lib/libMagick.so.9 #3 0xb57d9857 in Magick::Options::~Options () from /usr/lib/libMagick++.so.10 #4 0xb57d6725 in Magick::ImageRef::~ImageRef () from /usr/lib/libMagick++.so.10 #5 0xb57cbfe6 in Magick::Image::~Image () from /usr/lib/libMagick++.so.10 #6 0xb59ed7f3 in boost::python::objects::value_holder::~value_holder () from /var/lib/python-support/python2.5/PythonMagick/_PythonMagick.so #7 0xb581adea in ?? () from /usr/lib/libboost_python-gcc42-1_34_1-py25.so.1.34.1 #8 0xb6ce6f4f in ?? () from /usr/lib/libpython2.5.so.1.0 #9 0×0889a39c in ?? () #10 0xb6d8f7e0 in ?? () from /usr/lib/libpython2.5.so.1.0 #11 0xbf80d088 in ?? () #12 0xb6ce6c60 in ?? () from /usr/lib/libpython2.5.so.1.0 #13 0×00000000 in ?? () i was using pythonMagick which uses Magick++ wich uses ImageMagick. it was a bit irritating that Magick++ version 10 used ImageMagick version 9 instead of 10. after removing ImageMagick version 9 the problem was gone. no idea why it used the wrong version.

image manipulation with python

Wednesday, March 11th, 2009
in webapps you often need to manipulate images. create thumbnails, add shadows or borders, create captchas and many other things. usually i use imagemagick. it’s a very powerfull image manipulation tool with apis for many languages. i often experienced some difficulties when trying to use it with a specific language. there i usually almost no documentation for the apis and it looks like it’s not a lot used. many tend to call the imagemackig executable trough a os call.

how is it with python

i found a few image magick python modules. most were completely undocumented, unfinished or more or less uninstallable (at least on a 64 bit debian). the one who works properly is PythonMagick. i first tried to install the lattest version but due to some dependencies problems it wasn’t possible on my ubuntu system. there is a deb package available which was easyli installed with: sudo apt-get install python-pythonmagick it’s not the latest version but it worked ok, at least for my needs.

how do you use it

i found no documentation for it but it was quite simple to figure out the basic things. there is documentation for Magick++ which is a object oriented wrapper around imagemagick. all the image methods have short descriptions. to add a red 2 pixel border to an image for example you need following code: from PythonMagick import Image i = Image('example.jpg') # reades image and creates an image instance i.borderColor("#ff0000") # sets border paint color to red i.border("2x2") # paints a 2 pixel border i.write("out.jpg") # writes the image to a file the parameter “2×2″ of the border method is a geometry string used by imagemagick as input for many methods. another example who crops the image, flips it, adds an oil paint look and finally save it as a png. i = Image('example.jpg') i.crop("100x100+25+25") i.flip() i.oilPaint(2) i.save('out.png') if i find some spare time i will add a few more complex examples of how to use imagemagick from python.

python, i like the snake

Monday, March 2nd, 2009
90% of my development work during the last few years was done in java. i had to create a little project for the OLPC about a year ago. OLPC is using python very heavily and so i developed the tool in python. before that, i did only a few very simple things in python. a few weeks ago i started to develop webapplications in python. here are a few ideas and a list of packages/ software i used.

python versus java

compared with java, python is IMHO not as clean. it isn’t as “designed” as java is. but compared with, for example PHP, it’s a hyper clean and intuitive language. java is static typed and although also bytecode compiled like python, it needs an explicit compilation step. this makes developing python programs a bit more agile. for java and python there are lots of libraries/ packages available. as much as i have seen the python ones seems to be usually higher quality, but this can just be the selection i made and doesn’t need to be a general thing. the most important point in using python for webapps instead of java is that the python programs are usually fewer lines of code compared to the java programs. as much as i have seen till now i think this is also possible while having at least the same readability of the code like java programs. on the other hand you can write more easyli horrible code in python than in java. so python needs a bit more discipline. i will use python mainly for little not so critical projects, at least until i am fluent with it.

what did i use for webapps

with java i usually used an apache with mod_jk to connect to tomcat. i looked for a similar setup in python and found that there are a lot of possible ways but the most promising thing was the WSGI standard. there was a module for apache2 and so i was happy. WSGI is short of WebServer Gateway Interface and it sits somewhere between the webserver and python. compared with java where a application server is used who shares a common context for all servlets/ requests, WSGI-python scripts are loaded in a context for each process (apache processes i think), so caching and communication with other requests is not possible. but, if you are used to develop your applications with a cluster in mind it doesn’t hurt. you can use memcached for a cache.

because WSGI is a very low level interface it’s best to use a proper webframework. there are lots of them. the most are full blown frameworks and i am a bit allergic to them, i like it when i can put together the tools i want and doesn’t have to be pushed to use a certain way (forced, you are never. with full blown frameworks it’s the same as with certain girlfriends. they tend to give subtle hints about something but don’t force you, they are open to everyting but you get afraid what awaits you if you really go with something else then her preferred way). the only one i kind of liked was web.py. i’ts not the most beautiful but well built and it lets you do whatever you want if you like to. i use it mainly for mapping urls to code and webstuff like redirecting, send errors and stuff.

to access a database there are different packages available. i checked for orm mappers and found quite a lot of different implementations. the two best (as much or less as i can judge it) are Storm and SQLAlchemy. both are flexible, easy to use and have lots of functionality who doesn’t get in your way if not used. compared with hibernate it’s a dream.

to render html or whatever, i was looking for a good template language. there are many of them and i decided to use Jinja2. its the one i liked most but, hey it’s a template language, they are almost all usefull, properly built and support about the same featureset.

if i remember properly, thats all i needed, at least for the web aspect of the applications. python also has many very useful modules included, therefore you don’t need to include lots of modules. no crappy apache commons jars who give some functionality they missed to implement in the standard library.