最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

python多线程之thread

来源:动视网 责编:小采 时间:2020-11-27 14:28:17
文档

python多线程之thread

python多线程之thread:python 多线程之thread#! /usr/bin/env python # -*- coding:utf-8 -*- from threading import Thread import subprocess from Queue import Queue num_threads = 3 ips = ['10.108.100.174&#
推荐度:
导读python多线程之thread:python 多线程之thread#! /usr/bin/env python # -*- coding:utf-8 -*- from threading import Thread import subprocess from Queue import Queue num_threads = 3 ips = ['10.108.100.174&#


python 多线程之thread

#! /usr/bin/env python
# -*- coding:utf-8 -*-
from threading import Thread
import subprocess
from Queue import Queue
num_threads = 3
ips = ['10.108.100.174', '119.75.218.77', '127.0.0.1']
q = Queue()
def pingit(i, queue):
 while True:
 ip = queue.get()
 print "thread %s is pinging %s" % (i, ip)
 ret = subprocess.call('ping -c 3 %s' % ip, shell=True, stdout=open('/dev/null','w'))#正常则返回0,异常则返回1;stdout=open('/dev/null','w')屏蔽ping具体细节信息
 if ret != 0:
 print "%s is down" % ip
 queue.task_done()
for i in xrange(num_threads):#xrang比range好
 t = Thread(target=pingit, args=(i, q))
 t.setDaemon(True)#设置了setDaemon则线程会随着主线程关闭而关闭,python中,主线程结束后,会默认等待子线程结束后,主线程才退出
 t.start()
for ip in ips:
 q.put(ip)
print "main thread is waiting..."
q.join()
print "Done..."

文档

python多线程之thread

python多线程之thread:python 多线程之thread#! /usr/bin/env python # -*- coding:utf-8 -*- from threading import Thread import subprocess from Queue import Queue num_threads = 3 ips = ['10.108.100.174&#
推荐度:
标签: python 线程 th
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top