2008/08/25

Examples of Python threading and Queue

#!/usr/bin/env python
import threading,Queue,random,time,sys

class th(threading.Thread):

def __init__(self,threadName,queue):
threading.Thread.__init__(self,name=threadName)
self.name=threadName
self.Q=queue
print 'Name: %s started.'%(self.getName())

def run(self):
while 1:
num=self.Q.get(1)
if num==-1:
self.Q.task_done()
break
else:
print '%s sleep %f'%(self.name,num)
time.sleep(num)
print '%s wake after %f'%(self.name,num)
self.Q.task_done()
print '%s finish.'%self.name

thnum=2
Q=Queue.Queue(thnum*2)
for t1 in range(thnum):
th('thread%d'%t1,Q).start()

for t1 in range(10):
Q.put(random.random(),1)

for t1 in range(thnum):
Q.put(-1,1)
print 'wait...'
Q.join()


Do not forget to call Q.task_done() after each run of thread. Otherwise, Q.join() will be waiting for ever.

2008/08/07

Search hostname in knowhost file when hostname is hashed

ssh-keygen -F hostname [-f known_hosts_file]

2008/08/05

Extract files from rpm file

Check what is in rpm:


rpm -qlp filename.rpm

Extract


rpm2cpio filename.rpm |cpio -idmv

2008/08/04

Hosting Muliple Site Django under Nginx with WSGI

My site is very small and the hosting provider have strict limit on memory. I compile the Nginx like this:

./configure --prefix=$HOME/nginx_build --with-cpu-opt=pentium4 --add-module=./mod_wsgi --without-http_autoindex_module --without-http_auth_basic_module --without-http_charset_module --without-http_ssi_module --without-http_geo_module --without-http_map_module --without-http_proxy_module --without-http_memcached_module --without-http_rewrite_module --without-http_referer_module --without-http_upstream_ip_hash_module --without-http_browser_module --without-http_limit_zone_module --without-http_empty_gif_module --conf-path=$HOME/conf/nginx.conf

The most important thing that host Django by mod_WSGI is
add
wsgi_use_main_interpreter off;
wsgi_enable_subinterpreters on;
to your conf file. Mod_wsgi need to create distinct sub interpreters.

Remove the navbar of blogspot

Add
#navbar-iframe { height: 0px; visibility: hidden; display: none }
to your css. Do not hate me, google.