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)
정보 처리 속도를 높이려면 어떻게해야합니까? 효과적으로 스레드를 사용하는 방법은 무엇입니까? –
매우 주관적이고 광범위하며 여기에서 쉽게 대답 할 수 없습니다. 예를 들어, 주요 성능 병목 현상은 인터넷 연결 또는 연결하려는 사이트 중 하나 일 가능성이 큽니다. –
이 경우 사용할 수있는 최대 또는 유효한 스레드 수는 얼마입니까? –