找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4275|回复: 2

如何实现多线程

9

主题

18

帖子

18

积分

贫民

积分
18
doubledna 发表于 2016-12-23 09:10:07 | 显示全部楼层 |阅读模式
写了一个快速获取ip地址的小程序 但是只能单线程,查询时间长,如何多线程实现捏?
代码如下:
import socket
from time import ctime

def URLtoIP():
    start = input("Start:")
    end = input("End:")
    machineroom = raw_input("machineroom:")
    Dm = raw_input("Dm:")
    print ctime()
    for i in range(start, end + 1):
       oneurl= "vip" + str(i) + "." + str(machineroom) + "." + str(Dm)
       url=str(oneurl.strip())
       # print url
       try:
           ip =socket.gethostbyname(url)
           # print ip
           iplist.writelines(url + "\t\t" + str(ip)+"\n")
       except:
           # print "There is no IP these domain names "
           iplist.writelines(url + "\t\t" + " No IP " + "\n")
try:
    iplist=open("iplist.txt","w")
    URLtoIP()
    iplist.close()
    print "complete !"
except:
    print "ERROR !"

print ctime()

回复

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
lss89225 发表于 2017-2-9 17:44:45 | 显示全部楼层
本帖最后由 lss89225 于 2017-2-9 17:46 编辑

使用threading模块
import threading
thread_1 = threading.Thread(target=URLtoIP)
thread_1.start()
回复 支持 反对

使用道具 举报

0

主题

20

帖子

20

积分

贫民

积分
20
iacxc 发表于 2018-3-3 11:04:58 | 显示全部楼层
多个线程向同一个文件里写会乱套的。
用一个全局的Queue,最后再写文件吧。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表