Python Tutorial
thread.start_new_thread(function, args[, kwargs])Starts a new thread and returns identifier. here function is the name of the calling function and args is the list of function parameters as tuple and kwargs(keyword arguments) is optional parameter.
import thread import time import threading def display_message(thread_name, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print "%s: Count:%d / %s" % (thread_name, count, time.ctime(time.time())) try: thread.start_new_thread(display_message, ("Thread-1", 3, )) thread.start_new_thread(display_message, ("Thread-2", 2, )) except: print "Error" # print threading.enumerate() input = raw_input("do you want to run multithreading demo?(y/n)") while(input in ["y", "Y","yes", "Yes"]): passOutput:
$ python thread_test.py do you want to run multithreading demo?(y/n)y Thread-2: Count:1 / Sun Sep 3 14:11:12 2017 Thread-1: Count:1 / Sun Sep 3 14:11:13 2017 Thread-2: Count:2 / Sun Sep 3 14:11:14 2017 Thread-1: Count:2 / Sun Sep 3 14:11:16 2017 Thread-2: Count:3 / Sun Sep 3 14:11:16 2017 Thread-2: Count:4 / Sun Sep 3 14:11:18 2017 Thread-1: Count:3 / Sun Sep 3 14:11:19 2017 Thread-2: Count:5 / Sun Sep 3 14:11:20 2017 Thread-1: Count:4 / Sun Sep 3 14:11:22 2017 Thread-1: Count:5 / Sun Sep 3 14:11:25 2017
#!/usr/bin/python import random import time import threading def display_message(count): random_sec = random.randint(1,4) print "thread %d is going to sleep for %d seconds" % (count, random_sec) time.sleep(random_sec) print "thread %d is resuming back" % count for count in range(5): thr = threading.Thread(target=display_message, args=(count,)) thr.start()Output:
$ python thread_test1.py thread 0 is going to sleep for 3 seconds thread 1 is going to sleep for 1 seconds thread 2 is going to sleep for 3 seconds thread 3 is going to sleep for 4 seconds thread 4 is going to sleep for 3 seconds thread 1 is resuming back thread 0 is resuming back thread 2 is resuming back thread 4 is resuming back thread 3 is resuming back
#!/usr/bin/python import threading class Number(threading.Thread): def __init__(self, num): threading.Thread.__init__(self) self.num = num def run(self): if self.num % 2 == 0: print "%d is even number\n" % self.num else: print "%d is odd number\n" % self.num threads = [] while True: input = long(raw_input("Number(0 to exit): ")) if(input == 0): break; thread = Number(input) threads.append(thread) thread.start() for thr in threads: thr.join()Output:
$ python thread_test2.py Number(0 to exit): 8 8 is even number Number(0 to exit): 1 1 is odd number Number(0 to exit): 0
#!/usr/bin/python # coding=utf-8 import threading import time import random class UpdateFile(threading.Thread): def __init__(self, thread_name, msg, delay): threading.Thread.__init__(self) self.thread_name = thread_name self.msg = msg self.delay = delay def run(self): file_msg = ("Thread '%s': delay: %d, message: " "%s\n" % (self.thread_name, self.delay, self.msg)) print file_msg time.sleep(self.delay) thread_lock.acquire() f = open('file_name.txt', 'a+') f.write(file_msg) f.close() print("Thread '%s' is completed" % self.thread_name) thread_lock.release() thread_lock = threading.Lock() threads = [] count = 1 while True: random_sec = random.randint(1,6) input = raw_input("Message('q' to exit): ") if(input == 'q'): break; thread = UpdateFile(str(count), input, random_sec) threads.append(thread) thread.start() count += 1 for thr in threads: thr.join()Output:
$ python thread_test3.py Message('q' to exit): hi Thread '1': delay: 5, message: hi Message('q' to exit): how r u? Thread '2': delay: 1, message: how r u? Message('q' to exit): Thread '2' is completed Thread '1' is completedfile_name.txt will be created in current directory with below content
Thread '2': delay: 1, message: how r u? Thread '1': delay: 5, message: hi
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page