【发布时间】:2019-11-15 07:09:06
【问题描述】:
我是 python 多线程的绝对初学者。我的应用程序需要远程登录大约 200 台服务器,执行命令并返回响应。我为远程登录和处理响应创建了单独的类。我阅读了有关线程中的 GIL 和竞争条件的信息,但不确定它们是否会对我的代码产生影响。因为对于每个线程,我都在创建该类的一个新实例并访问该方法。所以从技术上讲,线程不会共享相同的资源。谁能解释我的假设是否正确,如果不是,请解释正确的做法?
主要方法:
if __name__ == "__main__":
thread_list = []
for ip in server_list: # server list contains the IP of hosts
config_object = Configuration () # configuration class has method for telnet device
thread1 = threading.Thread(target=config_object.captureconfigprocess, args=(ip))
thread_list.append(thread1)
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()
【问题讨论】:
标签: python-3.x multithreading race-condition gil