python 多线程

luochao7050838 2012-12-14 03:29:13
threads = []
times = 100
for i in range(0,times):
t = threading.Thread(target=connToServer())
threads.append(t)
for i in range(0,times):
threads[i].start()
time.sleep(1)
#for i in range(0,times):
# threads[i].join()
#connToServer()
我的代码是这样的,connToServer()函数里面有个while 1:的循环,为什么这个循环不退出,我的第二个线程就没运行啊?线程不是同步运行的吗
...全文
221 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tunaG 2012-12-19
  • 打赏
  • 举报
回复

import threading
import time
threads = []
times = 100
def connToServer(i):
    while(1):
        print i
        time.sleep(1)
        
for i in range(0,times):
    t = threading.Thread(target = connToServer, args = (i,))
    threads.append(t)
    
for i in range(0,times):    
    threads[i].start()
    time.sleep(1)

这样没有问题,你看看是不是哪写错了
tunaG 2012-12-19
  • 打赏
  • 举报
回复
你是想实现线程退出吗?
zjs100901 2012-12-14
  • 打赏
  • 举报
回复
t = threading.Thread(target=connToServer()) 这句红色的括号去掉试试。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧