2016-11-13 67 views
0

我使用硒和多重处理来产生四个不同的网站,我想运行特定于驱动程序生成的网站的函数。如何在使用python进行多处理时使用池特定的功能?

这与我当前的代码:

from multiprocessing import Pool 
from selenium import webdriver 

def gh(hosts): 
    driver = webdriver.Chrome(executable_path='./chromedriver') 
    driver.get(hosts) 
    html_source = driver.page_source 
     if 'ryan' in html_source: 
      print 'ryan' 
      doSomethingForRyan() 
     elif 'austin' in html_source: 
      print 'austin' 
      doSomethingForAustin() 
     elif 'travis' in html_source: 
      print 'travis' 
      doSomethingForTravis() 
     elif 'levi' in html_source: 
      print 'levi' 
      doSomethingForLevi() 
     else: 
      print '--NONE--' 

if __name__ == '__main__': 
    p = Pool(4) 

    hosts = ["http://ryan.com", "https://www.austin.com", "http://levi.com", "http://travis.com"] 
    p.map(gh, hosts) 

我得到的结果是一样的东西: 奥斯汀 奥斯汀 瑞安 奥斯汀

+1

快速术语引言:你在一个池中运行4个工人。 – tdelaney

+0

然后我如何在不同的池中运行不同的函数? – AlwaysSunny

+0

至少,尝试枚举结果以查看worker是否正在提升并出错:'for p.map(gh,hosts):print(result)'。工作人员可能会返回True/False成功/失败,并且您会在失败时得到异常。 – tdelaney

回答

0

编辑 - 解决

而不是从driver.page_source读取,从driver.current_url读取确保我可以运行website-spe功能。

if 'ryan' in driver.current_url: 
    print 'ryan' 
    doStuff()