2017-11-20 1 views
0

어떻게 문제도시 수가 매우 많은 경우 스레드를 올바르게 사용하는 방법은 무엇입니까?

warning: conflicting chdir during another chdir block 

를 해결하기 위해 내가 첨부 파일 와 모든 도시의 장소 및 폴더를 만들 어떻게 적절하게 코드를 최적화하고 정확한 작업을 구현하는거야? 파일이있는 폴더가 있는지 확인한 다음 기존 파일에 새 텍스트를 추가하는 방법은 무엇입니까?

require 'open-uri' 
require 'JSON' 
require 'thread' 

def scrape_instagram_city_page(page) 
    cityArray = [] 
    id = 0 
    begin 
     instagram_source = open(page).read 
     content = JSON.parse(instagram_source.split("window._sharedData = ")[1].split(";</script>")[0]) 
     locationName = content['entry_data']['LocationsDirectoryPage'][0]['city_info']['name'] 
     nextpage = content['entry_data']['LocationsDirectoryPage'][0]['next_page'] 
     Dir.mkdir("#{locationName}") 
     loop do 
      id +=1 
      instagram_source = open(page+"?page=#{id}").read 
      content = JSON.parse(instagram_source.split("window._sharedData = ")[1].split(";</script>")[0]) 
      locationsList = content['entry_data']['LocationsDirectoryPage'][0]['location_list'] 
      locationsList.each do |hash| 
       cityArray.push(hash['id'].to_i) 
      end 
      if nextpage == "null" 
       break 
      end 
     Dir.chdir("#{locationName}") do 
      fileName = "#{locationName}.txt" 
      File.open(fileName, 'w') do |file| 
       cityArray.each do |item| 
        file << "https://www.instagram.com/explore/locations/#{item}/\n" 
       end 
      end 
     end 
     end 
    rescue Exception => e 
     return nil 
    end 
end 

threads = [] 
city = ["https://www.instagram.com/explore/locations/c2269433/dhewng-thailand/","https://www.instagram.com/explore/locations/c2260532/ban-poek-thailand/","https://www.instagram.com/explore/locations/c2267999/ban-wang-takrai-thailand/","https://www.instagram.com/explore/locations/c2255595/ban-nong-kho-thailand/","https://www.instagram.com/explore/locations/c2252832/ban-na-khum-thailand/","https://www.instagram.com/explore/locations/c2267577/ban-wang-khaen-thailand/","https://www.instagram.com/explore/locations/c2248064/ban-khung-mae-luk-on-thailand/","https://www.instagram.com/explore/locations/c2243370/ban-hua-dong-kheng-thailand/","https://www.instagram.com/explore/locations/c2269271/chieng-sean-thailand/","https://www.instagram.com/explore/locations/c2256442/ban-nong-phiman-thailand/","https://www.instagram.com/explore/locations/c2246490/ban-khlong-khwang-thai-thailand/"] 
city.each do |page| 
    threads << Thread.new do 
     scrape_instagram_city_page "#{page}" 
    end 
end 

threads.each(&:join) 

답변

0

질문에 답하기 전에 사이트 스크랩은 사이트의 서비스 약관에 위배되는 경우가 많습니다. 이 사실을 확인하고 불법적 인 행위를하지 않았는지 확인해야합니다.

chdir에 의해 변경된 "현재 디렉터리"는 모든 스레드가 공유하는 프로세스 차원의 설정입니다. 이 때문에 두 스레드가 동시에 변경하려고하면 예외가 발생합니다. 그것은 당신이 만드는 스레드의 수와 아무 관련이 없습니다.

이 문제를 방지하려면 현재 디렉터리를 변경하지 마십시오.

def scrape_instagram_city_page(page) 
    cityArray = [] 
    id = 0 
    begin 
     instagram_source = open(page).read 
     content = JSON.parse(instagram_source.split("window._sharedData = ")[1].split(";</script>")[0]) 
     locationName = content['entry_data']['LocationsDirectoryPage'][0]['city_info']['name'] 
     nextpage = content['entry_data']['LocationsDirectoryPage'][0]['next_page'] 
     Dir.mkdir("#{locationName}") 
     loop do 
      id +=1 
      instagram_source = open(page+"?page=#{id}").read 
      content = JSON.parse(instagram_source.split("window._sharedData = ")[1].split(";</script>")[0]) 
      locationsList = content['entry_data']['LocationsDirectoryPage'][0]['location_list'] 
      locationsList.each do |hash| 
       cityArray.push(hash['id'].to_i) 
      end 
      if nextpage == "null" 
       break 
      end 
      fileName = "#{locationName}/#{locationName}.txt" 
      File.open(fileName, 'w') do |file| 
       cityArray.each do |item| 
        file << "https://www.instagram.com/explore/locations/#{item}/\n" 
       end 
      end 
     end 
    rescue Exception => e 
     return nil 
    end 
end 
+0

정보 처리 속도를 높이려면 어떻게해야합니까? 효과적으로 스레드를 사용하는 방법은 무엇입니까? –

+0

매우 주관적이고 광범위하며 여기에서 쉽게 대답 할 수 없습니다. 예를 들어, 주요 성능 병목 현상은 인터넷 연결 또는 연결하려는 사이트 중 하나 일 가능성이 큽니다. –

+0

이 경우 사용할 수있는 최대 또는 유효한 스레드 수는 얼마입니까? –

관련 문제