Member Avatar for sravan953

Hey guys,

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

Code:

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:

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:

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:

global a
a=1

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

#3 doubt:

On running:

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:

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

LOLWUT? 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:

global a
a=1

class myThread(threading.Thread):    
    def run(self):
        print(a)
        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

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.

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?

Member Avatar for 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?

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

Member Avatar for sravan953

Also, when I use:

myThread().start()

-it starts a thread, so, similarly, can I stop a thread by:

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?

Member Avatar for sravan953

I'm not yet int threds, but hope this will help you:
http://www.daniweb.com/forums/thread36752.html
http://bytes.com/groups/python/514796-how-force-thread-stop
http://docs.python.org/library/threading.html#module-threading

While reading the discussion at http://bytes.com/groups/python/514796-how-force-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!

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.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.