954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

SIMPLE threading tutorial

By AutoPython on Nov 15th, 2009 10:08 am

!USING PYTHON 3.1!

Hello DaniWeb! Today I'm going to be posting a simple threading tutorial. First of all, what is threading? Well, threading is just another way of doing a side task without interrupting the main program. Now here's a simple example. Let's say we are going to make a simple program. When the program starts it is going to start a timer, and it will show the time. Now, observe these 2, similar programs.

Good version:

import threading
import time

threadBreak = False
def TimeProcess():
    while not threadBreak:
        print (time.time() - startTime)

startTime = time.time()

threading.Thread(target = TimeProcess).start()

input()
threadBreak = True
quit()

Bad Version:

import threading
import time

threadBreak = False
def TimeProcess():
    while not threadBreak:
        print (time.time() - startTime)

startTime = time.time()

TimeProcess()

input()
threadBreak = True 
quit()


Now, you may notice that, the working version will exit after you press any key. The not working version, will not let you quit after pressing anything. That is because this command:

threading.Thread(target = FunctionName)

Will run that function, but the function won't take control over the main code. Okay, now I have some more explaining to do. Let's view the commented version of the code:

import threading
import time

threadBreak = False
def TimeProcess():
    while not threadBreak: ## it will run this as long as threadBreak is false
        print (time.time() - startTime) 

startTime = time.time() ## I'm not sure how this works, just learned the trick.

threading.Thread(target = TimeProcess).start() ## Starts the thread in the background. 

input()
threadBreak = True ## stops the thread
quit()


That's a simple form of threading. But here's a more functional version of the above program.

import threading
import time

print ("Press any key to start timer!")
input()

threadBreak = False
def TimeProcess():
    while not threadBreak:
        print (time.time() - startTime)

startTime = time.time()

threading.Thread(target = TimeProcess).start()

input()
threadBreak = True
input ()
quit()


Well that's a simple threading tutorial! Threading can have many more uses, and don't only limit yourself to this.

Ah, excellent. threading is a great thing to show people.... Im still stuck for another idea for a tutorial though.. Unless i do wxPython ones. I already have some of those at my site tho :P

Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

Yeah, I just can't stand it seeing only 3 tutorials for such a great language. I want this place to be a significant source for tutorials.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

Thanks for the threading, it is easier to understand when you see it. For more tutorials, how about the web? I'm using a php script in my wife's blog and would like to use Python.

What I don't know is:
How the php program is executed (how to execute the Python)
Where to variable mc_user_ip comes from, I think it is a return for the php program,
How do I see the return values,
How does the web pages's values get passed to the php program.

I've found some Python examples of getting the ip address, but the rest of it I haven't found.

soldner
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Hm, I am not familiar of using Python in the web. Try making a new thread on this and you'll probably get more help.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

Great tutorial, it is helpful..

Leyond
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

for python
i believe that you need to execute it (if windows) through the cmd window
hope that this helps

Thanks for the threading, it is easier to understand when you see it. For more tutorials, how about the web? I'm using a php script in my wife's blog and would like to use Python.

What I don't know is: How the php program is executed (how to execute the Python) Where to variable mc_user_ip comes from, I think it is a return for the php program, How do I see the return values, How does the web pages's values get passed to the php program.

I've found some Python examples of getting the ip address, but the rest of it I haven't found.

albruno
Newbie Poster
2 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

Excellent. Threading is a great thing to show people....thanks
Robin marsh :)

marshrobin1
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Here is little modified version to be more evident what it does:

## run with Python 3 from command line or double click
import threading
import time

def TimeProcess():
    while not threadBreak:
        print (time.time() - startTime)

input("""
Press enter key to start timer,
after some time push enter again to stop it!""")

threadBreak = False

startTime = time.time()

threading.Thread(target = TimeProcess).start()

input()
threadBreak = True
input ('Quit by enter')
quit()
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Does everyone know about Doug Hellmann's Python Module Of The Week. A good source for basic learning.
http://www.doughellmann.com/PyMOTW/threading/index.html#module-threading
http://www.doughellmann.com/PyMOTW/about.html

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 
startTime = time.time() ## I'm not sure how this works, just learned the trick.


This line gets the current system time (a form which doesn't reset to zero). The script calculates the difference between the program start time and the current time.

seanbp
Junior Poster
129 posts since Jul 2010
Reputation Points: 20
Solved Threads: 15
 

That should fill the screen with a lot of time output .... shouldn't there be a sleep statement somewhere to make it more dramatic?

twohot
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

I am thinking of writting a tut about threading and socket. ;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: