列表中每个项目的线程,为列表中的每个项目运行两个函数

时间:2014-07-15 15:06:02

标签: python multithreading

我将在此处开头,我是线程新手。我尝试了几种不同的方式,接近成功,但不是真的。

我有一个从文件中创建的列表:

with open(server_file, 'rU') as fi:
    servers = fi.read().split('\n')

我有两个函数grabdata()和runcheck(),我需要为列表中的每个项运行,每个对(grabdata然后运行检查)都在自己的线程中。

功能:

def grabdata():
    for server in servers:
        print "Checking: "+str(server)
        devnull = open(os.devnull, 'wb') # hide unnecessary output
        command = "wget -O tmp/servers/" +server + " " + server+"/avicapture.html#status"
        result = Popen(command, shell=True, stdout=subprocess.PIPE, stderr=devnull).communicate()[0]

def runcheck():
    for server in servers:
        file_path = current_dir+"/tmp/servers/"+server
        with open(file_path, 'r') as file:
            file_content = file.readlines()
            for x in file_content:
                match = re.search( r'In Progress \((.*)%\)', x)
                if match:
                    temp = match.group(1)
                    results = temp
                    if int(temp) >= 90 :
                        message = "\nServer: "+server+ "... {0}% 
                    print(message)

我如何正确地做到这一点?

0 个答案:

没有答案