Thursday, January 1, 2015

Basic Threading and Queueing

Happy 2015! Nothing like a fresh smell of Python script in the morning! :)

I wanted to get more practice on Threading and Queueing. So I went to my favorite site to look at standard library, Python Module of the Week, to look at some usage examples.

Here is the result:

# Reference: Python Module of the Week

# Threading:
# Queue:

# Step 1
# Basic Thread usage
import threading

def workers():
""" thread worker function """
print "hello "

thread = []
for i in range(5):
print "Thread " + str(i)
t = threading.Thread(target=workers)

# Step 2
# Basic Queue usage
import Queue

q = Queue.Queue()

for i in range(5):
print "Putting item in Queu: " + str(i)

while not q.empty():
print "Getting Queue"
print q.get()

# Step 3
# Combine Queue and Threading
switches = ["switch1", "switch2", "switch3"]

switchQueue = Queue.Queue()
for switch in switches:
print "Putting switches %s in Quque" % switch

def switchWorker(switch):
print "Working on: " + switch

print "\r\nStart to work on switches: "
threads = []
for switch in switches:
t = threading.Thread(target=switchWorker, args=(switch,))

print "Done!\r\n"

Here is the output of the script:

Thread 0
Thread 1
Thread 2
Thread 3
Thread 4
Putting item in Queu: 0
Putting item in Queu: 1
Putting item in Queu: 2
Putting item in Queu: 3
Putting item in Queu: 4
Getting Queue
Getting Queue
Getting Queue
Getting Queue
Getting Queue
Putting switches switch1 in Quque
Putting switches switch2 in Quque
Putting switches switch3 in Quque

Start to work on switches:
Working on: switch1
Working on: switch2
Working on: switch3


Happy Coding!


  1. Hi,
    I don't know if you would read this, if you do.... please reply.

    I am new to IT. I was fresh out of college and worked for nearly an year in help desk. Later, my employer pushed me into network operations team. I've started my CCNA Studies. But still I would like to ask you something.

    Am I starting at the right place by doing the CCna studies?
    Do I need to learn coding right away?
    I am not dealing with data centers, just branch offices(a lot of them). I get to work with some cisco 2800 or 2950 routers, cat switches, adding firewall rules, bluecoat and little F5. I felt lucky when i moved into networking this early But I don't want to lose track of the industry and stand somewhere in the dark later on. So, what do you suggest me to do in my network related studies?

    Also, let me know what you would do if you were in my shoes.Like, how do you start off? The path you would take, etc..

    I thought i should start a blog to track my studies, but haven't posted a single article yet. :(

    Btw, This blog is awesome.I mean it!:)I should say it is inspirational.

    Note : Am no native english speaker. So excuse any typos or mistakes in the above written para.

  2. Hi J, thank you for reading and the comments. I am very happy if this blog helped in any small way. You express your thoughts and opinion very well in English, by the way. I think personally, I would continue to retain networking knowledge as much as I can, if you need a label next to it to show to your employer, you can probably use CCNP. But overall, just retain solid routing and switching knowledge will go a long way. But make sure you go for the knowledge instead of just the passing grade. Also if you have opportunities to get your hands on Juniper, Arista, Cumulus, etc. make the full use of them.

    While you are working on networking things, think about how you would automate this in Python. It is so much more motivating to learn when you actually have a goal in mind. Show your progress to your manager, do the 'before and after' measurement to your employer. For example, it used to take you 30 min to provision a switch, now it takes 2 minutes. This will encourage them to sponsor your study down the line. Celebrate small victories, pad yourself on the back and get a latte whenever you feel like you have achieve a milestone, even if only you knew about it.

    Good luck and happy coding!