Check out this refresher on ways to add items to python lists
“Share your knowledge. It is a way to achieve immortality.” ― Dalai Lama XIV
Continue reading “Python List Methods: Adding Items”
Use pipes from multiple threads for bidirectional communication.
“The truth will set you free, but first it will piss you off.” ― Joe Klaas, Twelve Steps to Happiness
Invoking an external process from python and interacting with it can be quite tricky. This is especially true if the interaction is duplex i.e. involving both reading and writing to it. Such an interaction can cause deadlocks since both processes can end up waiting for output from the other. One way to avoid such deadlocks is to separate the reader and writer parts to different threads. We demonstrate such an approach in this article.
Continue reading “How to Avoid Deadlock when Calling External Command from Python”
Use subprocess.Popen for more control over the child process
“The world is a book and those who do not travel read only one page.” ― Augustine of Hippo
In a previous article, we looked at calling an external command in python using subprocess.call(). That method allows you to accomplish several tasks during the invocation:
Continue reading “Run External Command in Python Using Popen”
Learn about the various aspects of the subprocess module.
“I hate quotations. Tell me what you know.” ― Ralph Waldo Emerson, The Essays of Ralph Waldo Emerson
Do you want to run an external command from inside python? Maybe you want to startup a daemon process (no input or output), or perhaps invoke a command and capture its output?
Continue reading “How to Call an External Command in Python”
Boost your python expertise with these examples from itertools.
“Never put off till tomorrow what may be done day after tomorrow just as well.” ― Mark Twain
We are continuing with our series on python itertools. In this article, we cover compress, dropwhile, takewhile and groupby.
Continue reading “Python itertools – compress, dropwhile, takewhile, groupby”
A review of various methods in python to check for file existence.
“Knowledge is a treasure, but practice is the key to it.” ― Lao Tzu
Python offers several alternative ways of checking whether a file exists. Each of these ways come with several quirks. Examine each and pick the one that suits you.
Continue reading “How to Check Whether a File Exists in Python?”
A gentle introduction to python itertools
“Laughter is poison to fear.” ― George R.R. Martin, A Game of Thrones
Python provides a module called itertools which, as the name suggests, provides a bunch of conveniences for dealing with iterations and looping. While you could spend your entire python career without ever having to touch this module, trust me when I say your life will be enriched if you at least know about what is available in itertools.
Continue reading “Python itertools – count, cycle and chain”
Use locking to properly share resources across multiple threads
“You’re not obligated to win. You’re obligated to keep trying. To the best you can do everyday.” ― Jason Mraz
Resource management in multi-threaded programs is a tricky situation. Say, for example, you have a counter variable which is read and modified by multiple threads. If you do not exercise proper care, it is possible that inconsistent values are read and/or propagated. This leads to subtle and hard-to-debug errors.
Continue reading “Updating a File from Multiple Threads in Python”
A beginner’s guide to threading in python
“The truth is like salt. Men want to taste a little, but too much makes everyone sick.” ― Joe Abercrombie, The Heroes
Let us learn about python threading. Threading refers to executing a task in a separate thread of control, freeing up the main thread for other activities. It is very useful for executing time-consuming tasks without holding up the rest of the program. Typical applications include servers which handle each request in a thread and programs with a user-interface which execute complex tasks in a background thread.
Continue reading “Python Threading Tutorial”
The python with statement is damn useful! Learn the intricacies of the with statement.
“Accept who you are. Unless you’re a serial killer.” ― Ellen DeGeneres, Seriously… I’m Kidding
Resource acquisition and cleanup is a very crucial aspect of programs. Resources need to be properly cleaned and released as soon as their usage is complete. Failing which scare resources might not be available to other programs running on the same machine. This is especially true for long-running programs such as servers and daemons.
Continue reading “Proper Cleanup of Resources in Python using the With Statement”