miércoles, 1 de abril de 2009

Perro, el perro.

Perro en el Patio Olmos
Parece salido de un capítulo de Alejo y Valentina, jaja. Estaba afuera del Patio Olmos cuando esperábamos con mi hermano a otro amigo para ir al Cosquín Rock.
La otra noche lo encontré por Nueva Córdoba y me pareció que el collar decía 123456789 así que parece que los pibes lo tienen de juguete al pobre animal, jaja.

domingo, 29 de marzo de 2009

Python is my religion

Python Argentina pyar ultima cena
Python es una religión: Tremenda reconstrucción de la Última Cena por los chicos de PyAr en el último PyCamp. No los conozco a todos personalmente pero sí a la mayoría. Son co-listeros de Python Argentina. Entre ellos, compañeros de trabajo, de la facu, de fiestas nerd en Córdoba, etc. Muy muy grossa foto.

miércoles, 11 de marzo de 2009

Admin Van Buuren

Admin Van Buuren


Una pelotudez que se me vino a la cabeza una de las tantas veces que tuve que ingresar user: admin password: admin en el trabajo quedó plasmada en esta imagen.

sábado, 7 de marzo de 2009

Limiting memory usage of a python script

I've been running recently some python and java experiments with important memory requirements on a machine shared with other people, some of them with the same needs on that machine.
There were no restrictions on resources usage by a user or a process, so controlled memory usage was the user's responsibility.

Java has a parameter to limit the amount of memory the virtual machine can use. If it exceeds that value, the machine exits and the running program fails. It is specified with java -Xmx value.

I couldn't find something similar for python so I procrastinated a bit writing a bash script to control the processes.
I made a cycle that monitored the values from /proc/meminfo and killed the running process if some limit was reached.
The problem with that solution was that my experiments were not just one program, but many, controlled by a bash script, so this solution would just kill the bash script and not the process doing the important memory usage. So I added some extra code borrowed from the web to kill also the subprocesses. That made the code longer and more complex, leading to this:

control_run.sh:
PROGRAM=$*

#Also kills process children, and their children, and so on
KILL_CHILDREN=1

#Minimum free memory available (in Kb.)
MEM_FREE_LIMIT=300000

#Minimum free swap (in Kb.)
SWAP_FREE_LIMIT=500000

#Run the program in background and get its PID
$PROGRAM &
PID=$!

#While the program is alive
ps $PID > /dev/null
while [ $? -eq "0" ]; do


MEM_FREE=$(grep MemFree: /proc/meminfo | egrep [0-9]+ -o)
#echo "Free Memory: ${MEM_FREE}"

SWAP_FREE=$(grep SwapFree: /proc/meminfo | egrep [0-9]+ -o)
#echo "Free Swap: ${SWAP_FREE}"

if [ "${MEM_FREE}" -le ${MEM_FREE_LIMIT} ] || [ "${SWAP_FREE}" -le ${SWAP_FREE_LIMIT} ]; then

if [ "${MEM_FREE}" -le ${MEM_FREE_LIMIT} ]; then
echo "free memory limit reached, exiting...";
else
echo "free swap limit reached, exiting...";
fi

if [ "${KILL_CHILDREN}" -eq 0 ]; then
echo "killing process with id $PID"
kill $PID
else
#code based on http://www.unix.com/unix-dummies-questions-answers/5245-script-kill-all-child-process-given-pid.html
KILL_PIDS=$PID

CHILDREN=`ps -ef| awk '$3 == '$PID' { print $2 }'`
while [ "$CHILDREN" != "" ]; do
KILL_PIDS="$KILL_PIDS $CHILDREN"
OLD_CHILDREN=$CHILDREN
CHILDREN=''
for i in $OLD_CHILDREN; do
CHILDREN="$CHILDREN `ps -ef| awk '$3 == '$i' { print $2 }'`"
done
done

echo "killing process with id $PID and its children"

for i in $KILL_PIDS
do
echo killing $i
kill $i
done

fi
exit 1;
fi

sleep 1

ps $PID > /dev/null
done

echo "program finished"


I tested it with a small python script that just consumes memory:

memory_consumer.py:
import time
a = range(100000)
while True:
a += range(100000)
print "printing something"
time.sleep(1)


and some bash scripts to execute three instances concurrently:


memory_consumer_main.sh:
#!/bin/sh
./memory_consumer_child.sh


memory_consumer_child.sh:
#!/bin/sh
python memory_consumer.py &
python memory_consumer.py &
python memory_consumer.py



we would run:
$./control_run.sh ./memory_consumer_main.sh
printing something
printing something
printing something
printing something
printing something
printing something
printing something
printing something
printing something
printing something
printing something
printing something
printing something
printing something
printing something
free memory limit reached, exiting...
killing process with id 9348 and its children
killing 9348
killing 9349
killing 9350
killing 9351
killing 9353



Another possibility I found was setting the memory limit with ulimit.
We can set it from the command line and enable it for the bash session, or incluide the statements in the first lines of a bash script.

That will limit the allowed memory for any process

for example:
$ ulimit -v 40000
$ ulimit -H -v 40000


There, we limited the available memory for a process to 40Mb.
Then,
$ python memory_consumer.py
printing something
printing something
printing something
printing something
Traceback (most recent call last):
File "memory_consumer.py", line 4, in
a += range(100000)
MemoryError


While simple, the problem with this approach is that it will only kill the process exceeding the memory usage. If we are running just one isolated process, that's fine. But in my case, the batch would continue in an erroneous state and produce wrong results.
Any comment or suggestion, including "hey stupid, python has the option -blabla to do exactly that" will be appreciated.

miércoles, 26 de noviembre de 2008

Experimentando con Qtdesigner y PyQt

El fin de semana estuve probando hacer algún programita con interfaz gráfica usando python y QT. Para eso usé QTDesigner para editar la UI, y PyQT4 para usar la librería QT desde python.

Seguí este tutorial: http://www.cs.usfca.edu/~afedosov/qttut/ que aunque está viejo (es para PyQT3) la idea es la misma y se lo puede adaptar con algunos ejemplos para PyQT4 que hay dando vueltas.

El resultado fue este programita para setear algunos parámetros de mencoder y utilizarlo para capturar TV.

http://code.google.com/p/mtvcgui/

miércoles, 29 de octubre de 2008

Overclockeando la MSX

Todavía conservo mi primer computadora, una MSX Spectravideo SVI-728 que le compré a mi primo cuando se compró su XT.
Tenía solamente un intérprete de BASIC, con el que escribí mis primeras líneas de código (de ahí mis pésimas prácticas :P), y una lectora de cassettes, que usaba para cargar alguno de los cientos de juegos que tenía.

Cada tanto me agarra la nostalgia y me pongo a jugar juegos de esa epoca en emuladores en mi PC actual. Revisando una página encontré una utilidad para pasar a WAV los ROMs usados en los emuladores (los cassettes si se escuchan en un equipo de audio reproducen un sonido similar al de un modem dial up). Grabé un CD de audio con los mejores juegos generando el WAV al doble de bitrate del que se usaba en los cassettes y armé un cable para conectar un discman a la entrada de datos del teclado/motherboard/cpu/etc. A veces falla dando error de input/output, pero ajustando el volumen apropiadamente ahora se puede cargar un juego en la mitad de tiempo :D

Notar el Microsoft en la pantalla. Gracias Bill! Ahora odio tu Internet Explorer pero esta maquinola me trajo muchas horas de diversión! Todo bien con Stallman, pero por más que el Emacs tenga un tetris y tenga que hacer más o menos las mismas combinaciones de teclas para escribir un caracter que para saltar y patear en el Yie Ar Kung Fu, no se compara :P






martes, 28 de octubre de 2008

Mis primeros 0 bytes aportados al core de Plone

Como parte de las actividades locas de los viernes en menttes, esta vez, como hace ya un par de semanas, estuvimos tratando de resolver tickets para el "Plone Tune-Up". En este evento se tratan de solucionar problemas reportados al bug tracker de Plone.

Estuve trabajando en un problema javascriptoso que para variar, solo ocurría en Internet Explorer.
A un documento de Plone, se le puede activar una tabla de contenidos desde "Editar" -> "Configuración" y activar la TdC. Esto genera con javascript la tabla en base a los headlines del documento.
El problema reportado era que en IE7, los títulos que contenían una @ se transformaban dentro de la tabla en el texto del link hacia el headline. En realidad también ocurre con títulos que comienzan con urls, y en IE6 (lo pude reproducir en linux usando IEs4linux)

El javascript que generaba la tabla usaba JQuery, en particular las líneas que metían el texto y el enlace eran puramente JQuery. Parecía ser un bug en esta librería, pero este thread en su lista me demostró que en realidad era todo culpa de IE.
La tremenda solución consistió en cambiar el orden de dos líneas del código.

Screenshot con el problema:




Screenshot con el javascript modificado:



El ticket ya fue cerrado, "commiteado" y "mergeado":D

La verbosidad de este patch va perfecto con mi personalidad, pero igual espero poder aportar para el próximo tuneup algún parche un poco más interesante :)