Problem comprehending threading and functions...

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Problem comprehending threading and functions...

 
0
  #1
Aug 1st, 2009
Hey guys,

I have some trouble understanding global variables, threading, and functions...

Code:

  1. import threading
  2.  
  3. a=1
  4.  
  5. class myThread(threading.Thread):
  6. def run(self):
  7. global a
  8. print(a)
  9. a+=1
  10.  
  11. t=input("Specify the number of threads: ")
  12.  
  13. for b in range(t):
  14. myThread().start()
  15. raw_input()

#1 doubt:

  1. class myThread(threading.Thread):
  2. def run(self):
  3. global a
  4. print(a)
  5. a+=1

Why do I have to specify 'self' while defining the function? I usually don't do it if the function isn't in a class, then why if it is in a class?

#2 doubt:
  1. a=1
  2.  
  3. class myThread(threading.Thread):
  4. def run(self):
  5. global a
  6. print(a)
  7. a+=1

Why is variable 'a' first defined to be 1, and then globalized in the function specifically in the class? Why can't I do:

  1. global a
  2. a=1
  3.  
  4. class myThread(threading.Thread):
  5. def run(self):
  6. print(a)
  7. a+=1

#3 doubt:

On running:

  1. import threading
  2.  
  3. a=1
  4.  
  5. class myThread(threading.Thread):
  6. def run(self):
  7. global a
  8. print(a)
  9. a+=1
  10.  
  11. t=input("Specify the number of threads: ")
  12.  
  13. for b in range(t):
  14. myThread().start()
  15. raw_input()

-I get some numbers to be repeated...like if number of threads is, say 100:
  1. .
  2. .
  3. .
  4. 77
  5. 78
  6. 79
  7. 80
  8. 81
  9. 82
  10. 83
  11. 84
  12. 85
  13. 86
  14. 87
  15. 87'88
  16. 89
  17. 90
  18. .
  19. .
  20. .
  21.  

Thanks a billion gazillion guys...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Problem comprehending threading and functions...

 
0
  #2
Aug 1st, 2009
Originally Posted by sravan953 View Post
#1 doubt:
Why do I have to specify 'self' while defining the function? I usually don't do it if the function isn't in a class, then why if it is in a class?
LOLWUT? myThread is a class, so naturally you should be specifying self... I don't really understand what you're asking though...
Originally Posted by sravan953 View Post
#2 doubt:
Why is variable 'a' first defined to be 1, and then globalized in the function specifically in the class? Why can't I do:
  1. global a
  2. a=1
  3.  
  4. class myThread(threading.Thread):
  5. def run(self):
  6. print(a)
  7. a+=1
Because that's not how you use globals. You're supposed to identify a global variable as needed. So when your function gets "run", the command global a tells the program to pull in a reference of a from the global namespace.

And for your third doubt... I'm running the exact code with 100+ and not seeing repeats... so I'm not sure what you're talking about
Last edited by jlm699; Aug 1st, 2009 at 11:49 am.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 174
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 50
snippsat's Avatar
snippsat snippsat is offline Offline
Junior Poster

Re: Problem comprehending threading and functions...

 
0
  #3
Aug 1st, 2009
Why do I have to specify 'self' while defining the function? I usually don't do it if the function isn't in a class, then why if it is in a class
In a class a function is called a method.
An example.

  1. class SimpleClass(object):
  2.  
  3. def SayHello(self, string):
  4. '''
  5. * This is a function,but in a class it is called a method
  6. * Self is the class glue,and it also transport data between metods
  7. * Think of self as a carrier
  8. '''
  9. print 'Hi,' + string + ' nice to see you'
  10.  
  11. make_object = SimpleClass() #Class instance
  12. make_object.SayHello('Tom') #Call method,we give one argument(self dont count)
  13.  
  14. '''
  15. output-->
  16. Hi,Tom nice to see you
  17. '''

global a
a=1

class myThread(threading.Thread):
def run(self):
print(a)
a+=1global a
a=1

class myThread(threading.Thread):
def run(self):
print(a)
a+=1
a = 1 is in the global namespace(scope)
Then global a is doing nothing.

Maybe is better to take away threading and try to understand how classes work first?
Last edited by snippsat; Aug 1st, 2009 at 11:55 am.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: Problem comprehending threading and functions...

 
0
  #4
Aug 1st, 2009
Also, when "Specify the number of threads" is asked, if I enter around 100,000, after a few seconds, it says thread could not be created? Any ideas why?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Problem comprehending threading and functions...

 
0
  #5
Aug 1st, 2009
Originally Posted by sravan953 View Post
Also, when "Specify the number of threads" is asked, if I enter around 100,000, after a few seconds, it says thread could not be created? Any ideas why?
You're probably over-loading your cpu. There's no reason that you should ever be creating that many threads...
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: Problem comprehending threading and functions...

 
0
  #6
Aug 1st, 2009
Also, when I use:
  1. myThread().start()
-it starts a thread, so, similarly, can I stop a thread by:
  1. myThread().stop()
?? I tried, but it didn't work, and also I googled and found exceedingly complicated articles on it...so can anyone help me?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,442
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Problem comprehending threading and functions...

 
0
  #7
Aug 2nd, 2009
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: Problem comprehending threading and functions...

 
0
  #8
Aug 2nd, 2009
Originally Posted by evstevemd View Post
I'm not yet int threds, but hope this will help you:
http://www.daniweb.com/forums/thread36752.html
http://bytes.com/groups/python/51479...ce-thread-stop
http://docs.python.org/library/threa...dule-threading
While reading the discussion at http://bytes.com/groups/python/51479...ce-thread-stop , I came across processes...and I was just wondering what that is? Is it totally different from threading?

They Python Docs tutorial really got me confused about processes!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 944
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Problem comprehending threading and functions...

 
0
  #9
Aug 2nd, 2009
If your on windows there is a great way to tell what a process is. I cant quite remember how to do it on a *NIX system. But you go CTRL+ALT+DEL and then go to the processes tab, you will see a whole lot of processes, all of the programs running on your computer.

But in each of these processes there can be threads. So a thread is a process inside a process. Generally you make spawn new processes when you are launching another application or something similar and use threading for most other things.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,442
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Problem comprehending threading and functions...

 
0
  #10
Aug 3rd, 2009
a process is a program that runs with its own memory block and it own global variables. Processes don't share memory block, and cannot communicate directly except via inter process communication

Threads on other hand is "lightweight" process and share memory block and many other resources that processes don't share. To create a process you need new memory block, while thread share the parent thread's memory.

Single process can spawn many threads and as a matter of fact, a process is a single threaded or multithreaded.
Hope haven't confused you too!
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC