| | |
Problem comprehending threading and functions...
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hey guys,
I have some trouble understanding global variables, threading, and functions...
Code:
#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?
#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:
#3 doubt:
On running:
-I get some numbers to be repeated...like if number of threads is, say 100:
Thanks a billion gazillion guys...
I have some trouble understanding global variables, threading, and functions...
Code:
Python Syntax (Toggle Plain Text)
import threading a=1 class myThread(threading.Thread): def run(self): global a print(a) a+=1 t=input("Specify the number of threads: ") for b in range(t): myThread().start() raw_input()
#1 doubt:
Python Syntax (Toggle Plain Text)
class myThread(threading.Thread): def run(self): global a print(a) 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)
a=1 class myThread(threading.Thread): def run(self): global a print(a) 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)
global a a=1 class myThread(threading.Thread): def run(self): print(a) a+=1
#3 doubt:
On running:
Python Syntax (Toggle Plain Text)
import threading a=1 class myThread(threading.Thread): def run(self): global a print(a) a+=1 t=input("Specify the number of threads: ") for b in range(t): myThread().start() raw_input()
-I get some numbers to be repeated...like if number of threads is, say 100:
Python Syntax (Toggle Plain Text)
. . . 77 78 79 80 81 82 83 84 85 86 87 87'88 89 90 . . .
Thanks a billion gazillion guys...
•
•
•
•
#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?
myThread is a class, so naturally you should be specifying self... I don't really understand what you're asking though...•
•
•
•
#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)
global a a=1 class myThread(threading.Thread): def run(self): print(a) a+=1
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.
•
•
•
•
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
An example.
python Syntax (Toggle Plain Text)
class SimpleClass(object): def SayHello(self, string): ''' * This is a function,but in a class it is called a method * Self is the class glue,and it also transport data between metods * Think of self as a carrier ''' print 'Hi,' + string + ' nice to see you' make_object = SimpleClass() #Class instance make_object.SayHello('Tom') #Call method,we give one argument(self dont count) ''' output--> Hi,Tom nice to see you '''
•
•
•
•
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.
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...
Also, when I use:
-it starts a thread, so, similarly, can I stop a thread by:
?? I tried, but it didn't work, and also I googled and found exceedingly complicated articles on it...so can anyone help me?
Python Syntax (Toggle Plain Text)
myThread().start()
Python Syntax (Toggle Plain Text)
myThread().stop()
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
http://www.daniweb.com/forums/thread36752.html
http://bytes.com/groups/python/51479...ce-thread-stop
http://docs.python.org/library/threa...dule-threading
•
•
•
•
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
They Python Docs tutorial really got me confused about processes!
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.
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
Check out my Site | and join us on IRC | Python Specific IRC
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!
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!
![]() |
Similar Threads
- Fixed last problem but this ends in infinite loop, why? (Python)
- sniffing problem (Sending payload) in xp 64 bit system (C++)
- Threads in dll (C)
- Help with sphere member functions (C++)
- Number guessing game problem (C++)
- c++ functions to compare numbers (C++)
- callbacks and class member functions... (C++)
- JavaScript problem in FF (JavaScript / DHTML / AJAX)
- Virtual functions (C++)
- No idea how to do this problem... (C++)
Other Threads in the Python Forum
- Previous Thread: urlretreive
- Next Thread: devenv solution.sln /build
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array basic beginner book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary dynamic examples excel file filename float format ftp function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion recursive redirect refresh scrolledtext server software ssh stamp statictext statistics string strings table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame wxpython






