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.

http://pymotw.com/2/threading/index.html#module-threading
http://pymotw.com/2/Queue/index.html#module-Queue

Here is the result:

#
# eric@pythonicneteng.com
# Reference: Python Module of the Week http://pymotw.com/
#

# Threading: http://pymotw.com/2/threading/index.html#module-threading
# Queue: http://pymotw.com/2/Queue/index.html#module-Queue

##########
# Step 1
# Basic Thread usage
import threading

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

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

##########
# Step 2
# Basic Queue usage
import Queue

q = Queue.Queue()

for i in range(5):
print "Putting item in Queu: " + str(i)
q.put(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
switchQueue.put(switch)

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

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

print "Done!\r\n"
##########

Here is the output of the script:

$python threadingPractice.py
Thread 0
hello
Thread 1
hello
Thread 2
hello
Thread 3
hello
Thread 4
hello
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
0
Getting Queue
1
Getting Queue
2
Getting Queue
3
Getting Queue
4
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
Done!

$

Happy Coding!



2 comments:

  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.

    ReplyDelete
  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!

    Eric

    ReplyDelete