多进程
    多线程

    1. from threading import Thread
    2. from time import sleep
    3. class CookBook(Thread):
    4. def __init__(self):
    5. self.message = "Hello parallel Python"
    6. return super().__init__()
    7. def print_message(self):
    8. print(self.message, self.name, self.ident)
    9. def run(self):
    10. print("Thread Starting\n")
    11. x = 0
    12. while (x <10):
    13. self.print_message()
    14. # sleep(2)
    15. sleep(2)
    16. x += 1
    17. print("Thread Ended\n")
    18. if __name__ == "__main__":
    19. print("Process Started")
    20. hello_Python = CookBook()
    21. hello_Python.start()
    22. print("Process Ended")
    23. """
    24. output:
    25. (pythonparallel-jU7W1wcf) D:\projects\pythoncode\pythonparallel>python hellopythonwiththreads.py
    26. Process Started
    27. Thread Starting
    28. Process Ended
    29. Hello parallel Python Thread-1 11196
    30. Hello parallel Python Thread-1 11196
    31. Hello parallel Python Thread-1 11196
    32. Hello parallel Python Thread-1 11196
    33. Hello parallel Python Thread-1 11196
    34. Hello parallel Python Thread-1 11196
    35. Hello parallel Python Thread-1 11196
    36. Hello parallel Python Thread-1 11196
    37. Hello parallel Python Thread-1 11196
    38. Hello parallel Python Thread-1 11196
    39. Thread Ended
    40. """

    注意:永远不要留下任何线程在后台默默运行。