Threading in Python

Just another quick tip and trick for Python, and that is how you implement threading. This is surprisingly simple in Python, but basically it involves installing the threading library using pip

pip install threading
pip install threading
pip install threading

Enter fullscreen mode Exit fullscreen mode

From there the process is the following:

numThreads = 10
threadList = []
def RunTest(self, name):
print(f"Hello {name}")
for x in range(numThreads):
t = threading.Thread(target=RunTest, args=(x))
t.start()
threadList.append(t)
numThreads = 10
threadList = []

def RunTest(self, name):
    print(f"Hello {name}")

for x in range(numThreads):
    t = threading.Thread(target=RunTest, args=(x))
    t.start()
    threadList.append(t)
numThreads = 10 threadList = [] def RunTest(self, name): print(f"Hello {name}") for x in range(numThreads): t = threading.Thread(target=RunTest, args=(x)) t.start() threadList.append(t)

Enter fullscreen mode Exit fullscreen mode

This will then kick off each thread and let them run in the background, now the next logical question being how do I wait for a thread to complete, and that would be the following:

for t in threadList:
t.join()
for t in threadList:
    t.join()
for t in threadList: t.join()

Enter fullscreen mode Exit fullscreen mode

That’s really it, overall pretty easy.

原文链接:Threading in Python

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
Seeing your adorable smile is the absolute best part of my day.
看见你可爱的笑容绝对是我一天中最美好的事
评论 抢沙发

请登录后发表评论

    暂无评论内容