GhostiFi — Python, SQLAlchemy Threading

Reilly Chase
GhostiFi
Published in
3 min readNov 14, 2018

Overview

As promised in my initial blog post, I am continuing to publish the entire development process of GhostiFi, from concept, to sketch, to pseudocode, to actual code.

In this post (part 5), I will be sharing how I turned the threading pseudocode into actual Python:

Python Threading

I started off by researching how to do threading. I’ve done threading in Python a few times before. I made a GUI chatbot for PvPGN servers in PyQT4 once, and another time I wrote a script to upgrade firmware on 70+ Cisco switches.

I also made this video on how to thread Netmiko scripts:

However, I still didn’t feel that I fully understood how it works, so I started off by learning more about it.

I came across this blog post by Chris Kiehl, where he gave an example of an easier way to handle threading in Python, and it looked like a good idea:

So I decided to try it out. The code turned out to be much simpler than what I’ve done before, and it’s been working great so far for what I needed to do (spawn a new thread for each server create/destroy/rebuild).

SQLAlchemy Threading

Another topic that I researched was how to handle threading with SQLAlchemy. I had some experience having problems with this when I previously wrote a Flask API/SQLAlchemy backend for https://war2.info/analytics, a Warcraft II game report scraping analytics website. I had partially solved the problems that I had by using SQLAlchemy’s “scoped_session”, but never really understood the details of it.

The information that I found on SQLAlchemy threading was very limited, but the post below did reference using scoped_session again if you are threading SQLAlchemy. Although I’ll admit I still don’t really understand the problem or solution…

However, I moved forward using the new threading technique and scoping SQLAlchemy sessions, and I was able to get my code working!

So the project is now at the point where people can purchase the VPN, and my server.py script will know that it needs to create/destroy/rebuild a server for that user. It will spawn a new thread for each user who needs one of those tasks run. Right now those functions will simply print “Created!”, “Destroyed!”, or “Rebuilt!” respectively. Next step is to write the code to actually make those things happen instead of just printing it!

Closing thoughts

I hope this was helpful for anyone who is an aspiring programmer or entrepreneur to be able to follow along from my initial sketched design of a user interface for my product idea, to pseudo code, and finally actual code.

If you have any feedback on how I could improve this, please let me know in the comments section!

I am also looking for feedback on the concept itself, as well as beta testers. Please sign up for the newsletter at https://ghostifi.net if you are interested.

If you want to know when I release another post about building GhostiFi, you can follow me on Twitter: @_rchase_ or Medium: Reilly Chase

--

--