Avatar ·

if __name__ == "__main__" 的作用是什么?

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

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

# Threading examplenimport time, threadnndef myfunction(string, sleeptime, lock, *args):n    while 1:n        lock.acquire()n        time.sleep(sleeptime)n        lock.release()n        time.sleep(sleeptime)nif __name__ == "__main__":n    lock = thread.allocate_lock()n    thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))n    thread.start_new_thread(myfunction, ("Thread #: