• lock = threading.Lock()
    • 上锁lock.acquire()
    • 释放锁lock.release
    1. import threading
    2. import time
    3. num = 0
    4. lock = threading.Lock()
    5. def first():
    6. lock.acquire()
    7. for item in range(100000):
    8. global num
    9. num = num + 1
    10. pass
    11. lock.release()
    12. print("rask1:", num)
    13. pass
    14. def second():
    15. lock.acquire()
    16. for item in range(100000):
    17. global num
    18. num = num + 1
    19. pass
    20. lock.release()
    21. print("rask2:", num)
    22. pass
    23. if __name__ == '__main__':
    24. first_thread = threading.Thread(target=first)
    25. second_thread = threading.Thread(target=second)
    26. first_thread.start()
    27. second_thread.start()