python中使用线程时的循环时间

2024-10-02 12:25:20 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我为什么这个代码吗

import _thread,time

myStop = 0

def myTh():
  global myStop
  d = 0
  for _ in range(10000000):
    d += 0
  myStop += 1

_thread.start_new_thread(myTh,())
_thread.start_new_thread(myTh,())
_thread.start_new_thread(myTh,())

t = time.time()

while myStop!=3:
  # print("hello")
  pass

print(time.time() - t)

那要花更多的时间吗

import _thread,time

myStop = 0

def myTh():
  global myStop
  d = 0
  for _ in range(10000000):
    d += 0
  myStop += 1

_thread.start_new_thread(myTh,())
_thread.start_new_thread(myTh,())
_thread.start_new_thread(myTh,())

t = time.time()

while myStop!=3:
  print("hello")
 # pass

print(time.time() - t)

仅在while循环部件中进行更改

我真的很想知道它,因为我在我的项目中坚持这一部分

我试着用表达来代替书面陈述,但运气不好,如果书面陈述不是他们的话,总是需要更多的时间

谢谢


Tags: inimporthellonewfortimedefrange

热门问题