Skip to main content

Python run watchdog along with a flash app


The pre-requisite for understanding this blog post would be the following post. Please go through the following to set the context to understand what is discussed in this post.

Problem :
The flask app was running and was responding to a bunch of curl commands. This was single process thread that was working.
When the watchdog api was integrated and initiated with the flask program, the problem was that the watchdog thread was started and the flask app was never started. It was started only when the watchdog thread ended.

Since we want both the threads to be running, the watchdog process has to run in the background and the flask app to should run normally so that both functionalities are available for use.

Python thread and threading are the options available.
The "thread" starts a thread and waits for it to complete before proceeding with the rest of the flow to get executed.(But this is not what we want, right!!)
Threading starts a thread and runs in the background, the user won't know when the thread is ending but can track what is the status of the thread by using an isAlive check for the thread.

Thread:

Threading:

Important Note:

Please ensure that you are starting the thread and not waiting for it by any means.
Also, enable the daemonize option so that the background thread is closed when the main thread is stopped. 




Comments

Popular posts from this blog

The one where Raj went for Cycling (First time Experience - 50km circuit)

The Past: I met a friend Srini in violin class and got to know that he goes for cycling with a bunch of explorers once in a month. I was excited and told him I had a cycle but haven't done cycling and would love to try. So he told me he will inform when they go for a ride. The Present:  Srini texts me that they were planning a morning ride of approx 50-60km circuit. Even though I was excited, I was a bit nervous if I could push myself for that far. I did think of it for a while and I was like "If not now, then when??? ". I said yes. It was a Sunday morning that we started off. The weather in Bangalore was a big threat as it was raining pretty consistently for the past fortnight. We trusted few weather reports which stated that there is not going to be any rain in the morning and there might be slight rain in the afternoon. Srini started from HSR, two of friends joined in Bellandur, I joined in Kadubeesanahalli junction and two more joined in ISRO junction. The r...

Implementing a file monitoring script using python.

The idea is to implement a method where a user will be able to modify an xml file and the modifications will trigger a certain action in the script. This has to be implemented in python. WatchDogs is a python library that helps in file system monitoring. API reference : https://pythonhosted.org/watchdog/quickstart.html#quickstart Useful link :  http://brunorocha.org/python/watching-a-directory-for-file-changes-with-python.html The example scripts provided in the above links give a good headstart to play around with the file system and figure what is the exact use case that you want to achieve. Current use case:  The python script that I run is bundled as a container. There are mounted volumes in the container. So using an external script I am planning to modify the xml file and since the same file is modified inside the container, the python thread that is listening to the file modification event will trigger an action like sending a mail, make an entry to ...

Deploying a python web app to Google Cloud.

This is the place to start if you already have some experience in python, virtual env, flask. If you want to have some hands-on on how to deploy a simple python based flask app in Google Cloud. please refer to the following link. https://cloud.google.com/appengine/docs/standard/python/getting-started/python-standard-env The link provides all the instructions to get your python app up and running. Here are some points to be noted when you are doing this activity. The standard environment only works with python2.7 and not with python 3.5 and above. If you have already installed python3.5, please perform an alternate install of python2.7 as well.  If you're an active developer who had been developing using python3.5 and don't want to modify the python3.5 to python symlink on your machine, please perform the above testing activity in a python virtual env. You need to create the virtualenv folder using python2.7 explicitly using the following command  $ virtualenv -p /...