Friday, February 21, 2014

Synchronized decorator for Python

The following decorator provides synchronization for Python codes.
Reference


def synchronized(lock):
    """ Synchronization decorator. """

    def wrap(f):
        def newFunction(*args, **kw):
            lock.acquire()
            try:
                return f(*args, **kw)
            finally:
                lock.release()
        return newFunction
    return wrap

if __name__ == '__main__':
    from threading import Thread, Lock
    import time

    myLock = Lock()

    class MyThread(Thread):
        def __init__(self, n):
            Thread.__init__(self)
            self.n = n

        @synchronized(myLock)
        def run(self):
            """ Print out some stuff.

            The method sleeps for a second each iteration.  If another thread
            were running, it would execute then.
            But since the only active threads are all synchronized on the same
            lock, no other thread will run.
            """

            for i in range(5):
                print 'Thread %d: Start %d...' % (self.n, i),
                time.sleep(1)
                print '...stop [%d].' % self.n

    threads = [MyThread(i) for i in range(10)]
    for t in threads:
        t.start()

    for t in threads:
        t.join()

Filter forwarded messages in GMail

If you want to filter a message that has been forwarded to your GMail account by one of your other email services, then you can create a filter with Has Words field set to

deliveredto:your_email_service_mail_accountname_that_forwarded_message. 

You can set filters for example to archive such forwarded messages without showing them in your Inbox. You might want to do this for example, if you want to keep a record of all your emails in your GMail account but do not want to display duplicates when you are using an email client that display emails received in multiple accounts.

Continuously monitor GPU usage

 For nvidia GPUs do the follwing: nvidia-smi -l 1