Python 线程,with的作用(自动获取和释放锁Lock) import threading import time num= #全局变量多个线程可以读写,传递数据 mutex=threading.Lock() #创建一个锁 class Mythread(threading.Thread): def run(self): global num with mutex: #with Lock的作用相当于自动获取和释放锁(资源) ): #锁定期间,其他线程不可以干活 num+= print(num
转载:https://stackoverflow.com/questions/419163/what-does-if-name-main-do When your script is run by passing it as a command to the Python interpreter, python myscript.py all of the code that is at indentation level 0 gets executed. Functions and class