2013-02-13 4 views
0

GAE의 핸들러에서 웹 크롤링 함수를 호출하고 몇 개의 이미지를 검색 한 다음 표시합니다. 첫 번째 호출에서는 정상적으로 작동하지만 다음에 동일한 이미지를 모두 표시하면 마지막 하나가 중단 된 곳에서 크롤러가 시작됩니다. 내 전역 변수가 제대로 재설정되지 않는 것이 문제라고 생각합니다.Google App Engine에서 전역 변수 재설정이 작동하지 않습니다.

매번 앱을 다시 배포 할 때 처음 제대로 수행하지만 문제가 시작됩니다.

여기에 제 코드가 나와 있습니다. 분명히 말하면 제게 알려주세요.하지만 이해가되어야한다고 생각합니다. 여기

은 스크레이퍼 기능을 호출하는 핸들러 스크레이퍼 기능 여기

visited_pages = [] 
visit_queue = deque([]) 
collected_pages = [] 
collected_pics = [] 
count = 0 
pic_count = 0 

def scrape_pages(url, root_url, keywords=[], recurse=True): 
    #variables 
    max_count = 16 
    pic_num = 100 

    global count 
    global pic_count 
    global collected_pics 
    global collected_pages 

    print 'the keywords and url are' 
    print keywords 
    print url 

    #this is all of the links that have been scraped 
    the_links = [] 

    soup = soupify_url(url) 

    #only add new pages onto the queue if the recursion argument is true  
    if recurse: 
     #find all the links on the page 
     try: 
      for tag in soup.findAll('a'): 
       the_links.append(tag.get('href')) 
     except AttributeError: 
      return 

     try: 
      external_links, internal_links, root_links, primary_links = categorize_links(the_links, url, root_url) 
     except TypeError: 
      return 


     #change it so this depends on the input 
     links_to_visit = external_links + internal_links + root_links 

     #build the queue 
     for link in links_to_visit: 
      if link not in visited_pages and link not in visit_queue: 
       visit_queue.append(link) 

    visited_pages.append(url) 
    count = count + 1 
# print 'number of pages visited' 
# print count 

    #add pages to collected_pages depending on the criteria given if any keywords are given 
    if keywords: 
     page_to_add = find_pages(url, soup, keywords) 

#  print 'page to add' 
#  print page_to_add 
     if page_to_add and page_to_add not in collected_pages: 
      collected_pages.append(page_to_add) 


    pics_to_add = add_pics(url, soup) 
# print 'pics to add' 
# print pics_to_add 
    if pics_to_add: 
     collected_pics.extend(pics_to_add) 

    #here is where the actual recursion happens by finishing the queue 
    while visit_queue: 
     if count >= max_count: 
      return 

     if pic_count > pic_num: 
      return 

     link = visit_queue.popleft() 
#  print link 
     scrape_pages(link, root_url, keywords) 

# print '***done***' 
    ###done with the recursive scraping function here 

#here I just get a list of links from Bing, add them to the queue and go through them then reset all the global variables 
def scrape_bing_src(keywords): 
    visit_queue, the_url = scrape_bing.get_links(keywords, a_list = False) 
    scrape_pages(visit_queue.popleft(), the_url, keywords, recurse=True) 

    global collected_pics 
    global pic_count 
    global count 
    global visited_pages 
    global visit_queue 

    pic_count = 0 
    count = 0 
    visited_pages = [] 
    visit_queue = deque([]) 

    pics_to_return = collected_pics 
    collected_pics = [] 
    return pics_to_return 

입니다

#this just simply displays the images 
class Try(BlogHandler): 
    def get(self, keyword): 
     keyword = str(keyword) 
     keyword_list = keyword.split() 
     img_list = scraper.scrape_bing_src(keyword_list) 

     for img in img_list: 
      self.response.write("""<br><img src='""" + img + """'>""") 

     self.response.write('we are done here') 
+0

파이썬에서의 "Least Astonishment"의 복제본 : 변경 가능 기본 인수 (http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) –

+0

위대한 리소스가 있지만 꽤 많은 정보가 있고 그 중 일부는 상충되는 것처럼 보입니다. 이러한 변수를 설정하는 것이 더 좋을 것이라고 생각하십니까? None 또는 원래 클래스 외부에서 호출하고 그냥 반환하는 함수를 설정하는 것이 좋을까요? – clifgray

+0

두 가지 방법이 없습니다 : 함수 정의에'keywords = None'을 사용하십시오. –

답변

1

내부에 하나의 "서버"와 하나 개의 인스턴스, 당신이 아마를 실행하지 귀하의 코드 관리 콘솔에서 이미 발견 된 인스턴스 탭. 따라서 전화가 걸려 오더라도 다른 서버로 전환되거나 프로세스가 "다시 시작"될 수 있습니다 (더 많이 읽으려면 here을 읽을 수 있음). 워밍업 프로세스 중에 응용 프로그램은 디스크에서 메모리로 읽고 요청을 처리하기 시작합니다. 그래서 매번 전역 변수 값을 가진 새로운 미리 저장된 파이썬 인스턴스를 얻습니다.

귀하의 경우에는 memcache을 사용하는 것이 더 좋습니다.

+0

아니요 전역 변수가 인스턴스간에 유지되는 것을 원하지 않습니다. 나는 단지 그것을 사용하는 것으로부터 글로벌로 바꾸어야한다고 생각한다. – clifgray

+0

전역 변수가 인스턴스간에 "유지"되지 않습니다. 그러나 응용 프로그램이 인스턴스에로드되면 프로그램은 종료 될 때까지 계속 실행됩니다. 즉, 처리기가 요청 처리를 완료 한 후에 전역 변수가 살아 있음을 의미합니다. 당신이 여기에 글로벌 데이터를 원하지 않는 것처럼 보입니다. –

관련 문제