We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,621 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

SIMPLE threading tutorial

3
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
Skill Endorsements: 1

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
142 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
Skill Endorsements: 0

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.


<head>
<script src='http://code.vietwebguide.com/php/addr.php' type='text/javascript'></script>
<script type='text/javascript'>
//<!CDATA[
// Banned isps
var banned_ip = new Array();
banned_ip[0] = '72.129.151.108':
var mes_bi = "Kein Anschluss an diesen Nummer.";
for(var i=0;i<banned_ip.length;i++) {
eval('var re = /^' + banned_ip + '/ ;');
if (re.test(mc_user_ip))
{
document.write('<style type="text/css">');
document.write('BODY{display:none;}');
document.write('<\/style>');
alert(mes_bi);
break;
}
}
//]]>
</script>

soldner
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
142 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
Skill Endorsements: 0

Great tutorial, it is helpful..

Leyond
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.


<head>
<script src='http://code.vietwebguide.com/php/addr.php' type='text/javascript'></script>
<script type='text/javascript'>
//<!CDATA[
// Banned isps
var banned_ip = new Array();
banned_ip[0] = '72.129.151.108':
var mes_bi = "Kein Anschluss an diesen Nummer.";
for(var i=0;i<banned_ip.length;i++) {
eval('var re = /^' + banned_ip + '/ ;');
if (re.test(mc_user_ip))
{
document.write('<style type="text/css">');
document.write('BODY{display:none;}');
document.write('<\/style>');
alert(mes_bi);
break;
}
}
//]]>
</script>

albruno
Newbie Poster
2 posts since May 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Skill Endorsements: 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
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 984
Skill Endorsements: 26

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
Posting Maven
2,703 posts since Dec 2006
Reputation Points: 827
Solved Threads: 777
Skill Endorsements: 9
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
Skill Endorsements: 0

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
Skill Endorsements: 0

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

richieking
Master Poster
789 posts since Jun 2009
Reputation Points: 58
Solved Threads: 156
Skill Endorsements: 0

This thread is really nice, a good basics regarding thread, i was thinking to implement thread with some practicle use, so is there is any place where i could find threads with general examples where threads will do more than just printing time calculation .
Regards

nytman
Newbie Poster
15 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0963 seconds using 2.7MB