2015-07-28 37 views
0

我想將超時應用於我通常工作正常的方法之一。我已經試過這個簡單的打印功能的代碼,一切正常,但現在我得到錯誤。你能告訴我問題在哪裏嗎?Python - 多線程pickle.PicklingError錯誤

方法self.check_flights應運行最大__TIMEOUT__秒。

下面的代碼是在這是在一個類中的方法some_class

  try: 
       p = multiprocessing.Process(target=self.check_flights, args=(destination, start_date , end_date_2)) 
       p.start() 
       p.join(__TIMEOUT__) 
       if p.is_alive(): 
        print 'TIMEOUT' 
        p.terminate() 
        p.join() 

      except Exception as e: 
       raise e 

>ERROR: pickle.PicklingError: Can't pickle <class __main__.some_class at 0x02DD10A0>: it's not the same object as __main__.some_class 

編輯:該超時的解決方案是從THIS SO回答

編輯2:爲了更詳細說明它,我附加另一行的錯誤:

Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
    File "C:\Python27\lib\multiprocessing\forking.py", line 381, in main 
    self = load(from_parent) 
    File "C:\Python27\lib\pickle.py", line 1378, in load 
    return Unpickler(file).load() 
    File "C:\Python27\lib\pickle.py", line 858, in load 
    dispatch[key](self) 
    File "C:\Python27\lib\pickle.py", line 880, in load_eof 
    raise EOFError 
EOFError 
+0

能否請您複製/粘貼或共享莫名其妙完整的類? – Geeocode

+0

您是否使用過'if __name__ =='__main __':'? – Geeocode

+0

@GyörgySolymosi是的,我用過它。這是整個代碼http://pastebin.com/uG0qSvhU –

回答

0

這實際上是一些多進程問題,而multiprocessing嘗試酸洗某些對象t o有效平行。

爲什麼你使用如此複雜的timout方式,如果你有直接訪問你的方法,因此你可以在其中包含一個超時。

當你的只有時間敏感的方法是load_whole_page方法,我會修改你的代碼段如下簡單地離開了多進程的基本元素:

def load_whole_page(self,destination,start_date,end_date): 
    deb() 
    . 
    . 
    . 

    header = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'header'))) 
    footer = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'footer'))) 


    results = [] 
    # setting time threshold 
    threshold = 10 
    t = time() 
    while True: 
     # cheking if we reach the time we don't want to exceed. 
     if (t - time()) >= threshold: 
      with open('log.txt', 'a') as f: 
       f.write(str(e)) 
       f.write(traceback.format_exc()) 
       print 'TIMEOUT ERROR' 
      break 

     wait_2.until(wait_for_more_than_n_elements((By.CSS_SELECTOR, "div.flightbox"), len(results))) 
+0

1.我是新手,我不知道該怎麼做。 –

+0

2.我也不知道,在我發佈的問題中提出了這個問題。你能給我一個建議如何改進代碼? –

+0

你會如何在功能內部產生超時?我雖然這是一個更簡單的方法,因爲我想在打開第二個線程時調用某個函數。 –