2017-11-08 10 views
0

이것은 URL의 로컬 호스트를 호출하는 프로그램 입력 다중 URL이있다 : 8888/API/V1/러파이썬 프로그램 호출

에 1 + 시간 복용 프로그램 ok를 실행하지만 다른 api를 차단합니다. 는 다른 실행할 때 어떤 API는 기존 API를 마지막까지 작동하지 않습니다 그래서 비동기 그래서 난 같은 프로그램

@tornado.web.asynchronous 
    @gen.coroutine 
    @use_args(OrgTypeSchema) 
    def post(self, args): 
     print "Enter In Crawler Match Script POST" 
     print "Argsssss........" 
     print args 
     data = tornado.escape.json_decode(self.request.body) 
     print "Data................" 
     import json 
     print json.dumps(data.get('urls')) 
     from urllib import urlopen 
     from bs4 import BeautifulSoup 
     try: 
       urls = json.dumps(data.get('urls')); 
       urls = urls.split() 

       import sys 

       list = []; 

       # orig_stdout = sys.stdout 
       # f = open('out.txt', 'w') 
       # sys.stdout = f 
       for url in urls: 
        # print "FOFOFOFOFFOFO" 
        # print url 
        url = url.replace('"'," ") 
        url = url.replace('[', " ") 

        url = url.replace(']', " ") 
        url = url.replace(',', " ") 
        print "Final Url " 
        print url 
        try: 
         site = urlopen(url) .............. 

답변

0

귀하의 게시물 방법은 100 % 동기 달성 할 수있는 방법이 프로그램을 실행하려면. site = urlopen(url)을 비동기로 설정해야합니다. 토네이도에는 async HTTP client이 있습니다. 또한 좋은 예가 here입니다.

+0

여전히 실행 중일 때 다른 API 호출을 차단 중 프로그램이 – user2775366

+0

아직 차단중인 경우 비동기 http 클라이언트를 사용하여 새 코드를 추가 할 수 있습니까? – Fian

0

차단 이유는 urllib입니다.

토네이도는 AsyncHTTPClient이라는 비 차단 클라이언트를 제공합니다.이 클라이언트는 사용해야합니다.

이처럼 사용

from tornado.httpclient import AsyncHTTPClient 

@gen.coroutine 
@use_args(OrgTypeSchema) 
def post(self, args): 
    ... 
    http_client = AsyncHTTPClient() 
    site = yield http_client.fetch(url) 
    ... 

나는 함수 내부에서 모듈을 가져올 수 없습니다입니다 지적하고 싶은 다른 일을. 하지만 차단 이유는 아니지만 모든 가져 오기를 파일의 맨 위에 놓는 것보다 느립니다. this question을 읽으십시오.