Showing posts with label Multithreading. Show all posts
Showing posts with label Multithreading. Show all posts

Thursday, 6 October 2016

Python safeguards


  • Never create a resource with open() or with open() inside run method of a worker thread. Create a resource in the main thread and pass the reference to the resource to the worker instead.
  • with open() is better than open() for writers because it closes the writer automatically when the control exits its scope. This flushes all write buffer to the disk.
  • If open() is used for write(), explicitly call f.close() or f.flush() as you would normally do in Java.

Friday, 30 May 2014

Do away with System.out.println and Logger.info and your code now runs really faster!

I wrote a 10-threaded script last evening and was astounded to note that the performance hadn't really improved as I had expected. I knew for sure that I had parallelized all computation to get that 10x boost in speed from my initial single-threaded code and I was struck by this weird idea that 'sysout' and Logger.info were to blame. 

My good friend told me that if I were printing feedback out to the console on every single operation, that was a good reason why my code didn't perform as had been anticipated initially. I removed all those statements and bang! the code now run smooth and fast.