Avatar ·

if __name__ == “__main__” 的作用是什么?

📁 装饰师, 方法, 多处理器, 解析

在调用 if __name__ == "__main__": 时会发生什么?

# Threading Example
import time, thread

def myFunction(string, sleeptime, lock, *args):
    while 1:
        lock.acquire()
        time.sleep(sleeptime)
        lock.release()
        time.sleep(sleeptime)

if __name__ == "__main__":
    lock = thread.allocate_lock()
    thread.start_new_thread(myFunction, ("Thread #: 1", 2, lock))
    thread.start_new_thread(myFunction,