1from threading import Thread
2from time import sleep
3
4def threaded_function(arg):
5 for i in range(arg):
6 print("running")
7 sleep(1)
8
9
10if __name__ == "__main__":
11 thread = Thread(target = threaded_function, args = (10, ))
12 thread.start()
13 thread.join()
14 print("thread finished...exiting")
1# A minimal threading example with function calls
2import threading
3import time
4
5def loop1_10():
6 for i in range(1, 11):
7 time.sleep(1)
8 print(i)
9
10threading.Thread(target=loop1_10).start()
11
12# A minimal threading example with an object
13import threading
14import time
15
16
17class MyThread(threading.Thread):
18 def run(self): # Default called function with mythread.start()
19 print("{} started!".format(self.getName())) # "Thread-x started!"
20 time.sleep(1) # Pretend to work for a second
21 print("{} finished!".format(self.getName())) # "Thread-x finished!"
22
23def main():
24 for x in range(4): # Four times...
25 mythread = MyThread(name = "Thread-{}".format(x)) # ...Instantiate a thread and pass a unique ID to it
26 mythread.start() # ...Start the thread, run method will be invoked
27 time.sleep(.9) # ...Wait 0.9 seconds before starting another
28
29if __name__ == '__main__':
30 main()
1from multiprocessing.pool import ThreadPool
2
3def stringFunction(value):
4 my_str = 3 + value
5 return my_str
6
7
8def stringFunctio(value):
9 my_str = 33 + value
10 return my_str
11
12
13
14pool = ThreadPool(processes=1)
15
16
17thread1 = pool.apply_async(stringFunction,(8,))
18thread2 = pool.apply_async(stringFunctio,(8,))
19
20return_val = thread1.get()
21return_val1 = thread2.get()
1#!/usr/bin/python
2
3import thread
4import time
5
6# Define a function for the thread
7def print_time( threadName, delay):
8 count = 0
9 while count < 5:
10 time.sleep(delay)
11 count += 1
12 print "%s: %s" % ( threadName, time.ctime(time.time()) )
13
14# Create two threads as follows
15try:
16 thread.start_new_thread( print_time, ("Thread-1", 2, ) )
17 thread.start_new_thread( print_time, ("Thread-2", 4, ) )
18except:
19 print "Error: unable to start thread"
20
21while 1:
22 pass
1from multiprocessing.pool import ThreadPool as Pool
2
3pool_size = 10
4pool = Pool(pool_size)
5
6results = []
7
8for region, directory_ids in direct_dict.iteritems():
9 for dir in directory_ids:
10 result = pool.apply_async(describe_with_directory_workspaces,
11 (region, dir, username))
12 results.append(result)
13
14for result in results:
15 code, content = result.get()
16 if code == 0:
17 # ...
1# =============================================================================
2# Inhertance
3# =============================================================================
4class A:
5 def feature1(self):
6 print('Feature 1 in process...')
7 def feature2(self):
8 print('Feature 2 in process...') #Pt.1
9
10class B:
11 def feature3(self):
12 print('Feature 3 in process...')
13 def feature4(self):
14 print ('Feature 4 in process...')
15
16a1 = A()
17
18a1.feature1()
19a1.feature2()
20
21a2 = B()
22
23a2.feature3()
24a2.feature4()
25# THE ABOVE PROGRAM IS A PROGRAM WITHOUT USING INHERITANCE
26
27# WITH THE USE OF INHERITANCE IS BELOW
28class A:
29 def feature1(self):
30 print('Feature 1 in process...')
31 def feature2(self):
32 print('Feature 2 in process...')
33
34class B(A):
35 def feature3(self):
36 print('Feature 3 in process...') # Pt.2
37 def feature4(self):
38 print ('Feature 4 in process...')
39
40a1 = A()
41
42a1.feature1()
43a1.feature2()
44
45a2 = B()
46
47a2.feature3()
48a2.feature4()
49