943,925 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 713
  • Python RSS
Aug 1st, 2009
0

Problem comprehending threading and functions...

Expand Post »
Hey guys,

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

Code:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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...
Similar Threads
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Aug 1st, 2009
0

Re: Problem comprehending threading and functions...

Click to Expand / Collapse  Quote originally posted by sravan953 ...
#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...
Click to Expand / Collapse  Quote originally posted by sravan953 ...
#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:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Aug 1st, 2009
0

Re: Problem comprehending threading and functions...

Quote ...
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.

python Syntax (Toggle Plain Text)
  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. '''

Quote ...
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.
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is offline Offline
771 posts
since Aug 2008
Aug 1st, 2009
0

Re: Problem comprehending threading and functions...

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?
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Aug 1st, 2009
0

Re: Problem comprehending threading and functions...

Click to Expand / Collapse  Quote originally posted by sravan953 ...
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...
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Aug 1st, 2009
0

Re: Problem comprehending threading and functions...

Also, when I use:
Python Syntax (Toggle Plain Text)
  1. myThread().start()
-it starts a thread, so, similarly, can I stop a thread by:
Python Syntax (Toggle Plain Text)
  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?
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Aug 2nd, 2009
0

Re: Problem comprehending threading and functions...

Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Aug 2nd, 2009
0

Re: Problem comprehending threading and functions...

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!
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Aug 2nd, 2009
0

Re: Problem comprehending threading and functions...

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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Aug 3rd, 2009
0

Re: Problem comprehending threading and functions...

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!
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: urlretreive
Next Thread in Python Forum Timeline: devenv solution.sln /build





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC