Python —— (4)多线程 Posted on 2017-09-17 | In others | | 本文总阅读量 次 线程 & 进程 thread类 threading模块 123456789101112131415161718192021222324252627282930#coding=utf-8import threadingimport timeimport requestsimport sysdef funl(): time_start = time.time() r = requests.get(url='http://www.baidu.com') times = time.time()=time_start sys.stdout.write('Status:%s---%s---%s\n'%(r.status_code, times, time.strftime('%H:%M:%S')))def main(): threads = [] threads_count = 10 for i in range(threads_count): t = threading.Thread(target=funl, args=()) threads.append(t) for i in range(threads_count): threads[i].start() for i in range(threads_count): threads[i].join()if __name__ == '__main__': main() 1234567891011121314151617181920212223242526272829303132333435#coding=utf-8import threadingimport Queueclass DoRun(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self._queue = queue def run(self): while not self._queue.empty(): ip = self._queue.get() print ipdef main(): threads = [] threads_count = 10 queue = Queue.Queue() for i in range(1, 255): queue.put('106.42.25.' + str(i)) for i in range(threads_count): threads.append(DoRun(queue)) for i in threads: i.start() for i in threads: i.join()if __name__ == '__main__': main() refer:Python教程 - 廖雪峰的官方网站 梦想还是要有的,万一实现了呢? 赏 微信打赏 支付宝打赏 Post author: Edward Choi Post link: https://edwardchoijc.github.io/python-多线程.html Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.