多进程
多线程
from threading import Thread
from time import sleep
class CookBook(Thread):
def __init__(self):
self.message = "Hello parallel Python"
return super().__init__()
def print_message(self):
print(self.message, self.name, self.ident)
def run(self):
print("Thread Starting\n")
x = 0
while (x <10):
self.print_message()
# sleep(2)
sleep(2)
x += 1
print("Thread Ended\n")
if __name__ == "__main__":
print("Process Started")
hello_Python = CookBook()
hello_Python.start()
print("Process Ended")
"""
output:
(pythonparallel-jU7W1wcf) D:\projects\pythoncode\pythonparallel>python hellopythonwiththreads.py
Process Started
Thread Starting
Process Ended
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Hello parallel Python Thread-1 11196
Thread Ended
"""
注意:永远不要留下任何线程在后台默默运行。