2015-07-21 19 views
10

Ứng dụng của tôi dựa trên:Python kiểm tra selen bị mắc kẹt trong urlopen

  • Python 3
  • Django 1,8
  • Weasyprint
  • Selenium

Nó chạy hoàn hảo trên dev và sản xuất môi trường , nhưng không phải trong khi thử nghiệm với selen.

Sử dụng tính năng xóa, tôi tạo tệp PDF từ HTML, thư viện này sử dụng urllib để tải xuống CSS (ví dụ: http://localhost:8081/static/lib/bootstrap/css/bootstrap.min.css), nhưng nó bị treo (không có lỗi, chỉ bị kẹt) trong khi mở.

Nếu tôi nhập url này trực tiếp trong trình duyệt của mình trong khi bị treo, CSS sẽ được hiển thị.

lệnh được sử dụng:

./manage.py test tests.test_account.HomeNewVisitorTest 

phần có liên quan của bài thi:

from selenium import webdriver 

class HomeNewVisitorTest(StaticLiveServerTestCase): 
    def setUp(self): 
     if TEST_ENV_FIREFOX: 
      self.driver = webdriver.Firefox() 
     else: 
      self.driver = webdriver.PhantomJS() 
     self.driver.set_window_size(1440, 900) 

    def tearDown(self): 
     try: 
      path = 'worksites/' + self.worksite_name.lower() 
      os.rmdir(settings.MEDIA_ROOT + path) 
     except FileNotFoundError: 
      pass 
     super().tearDown() 

    def test(self): 
     d = self.driver 
     d.get(self.get_full_url('home')) 
     d.find_element_by_css_selector('.btn-success[type=submit]').click() 

Theo quan điểm của tôi:

# Generate PDF for contact directory 
    template = get_template("pdf/annuaire.html") 
    context = {"worksite": worksite} 
    html = template.render(RequestContext(self.request, context)) 
    base_url = self.request.build_absolute_uri("/") 
    pdf = weasyprint.HTML(string=html, base_url=base_url) 
    pdf.write_pdf(directory + '/annuaire.pdf') 

Đây là bãi chứa thread khi bị mắc kẹt:

Fatal Python error: Aborted 

Thread 0x0000000106f92000 (most recent call first): 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 374 in readinto 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 313 in _read_status 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 351 in begin 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1171 in getresponse 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 1185 in do_open 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 1210 in http_open 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 441 in _call_chain 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 481 in _open 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 463 in open 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 161 in urlopen 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/urls.py", line 276 in default_url_fetcher 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/urls.py", line 311 in fetch 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/contextlib.py", line 59 in __enter__ 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/__init__.py", line 297 in _select_source 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/contextlib.py", line 59 in __enter__ 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/__init__.py", line 223 in __init__ 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/css/__init__.py", line 198 in find_stylesheets 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/css/__init__.py", line 448 in get_all_computed_styles 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/document.py", line 312 in _render 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/__init__.py", line 132 in render 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/weasyprint/__init__.py", line 164 in write_pdf 
    File "/Users/sebcorbin/Sites/planexo/worksite/views.py", line 111 in done 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 357 in render_done 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 730 in render_done 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 300 in post 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 686 in post 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/views/generic/base.py", line 89 in dispatch 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/formtools/wizard/views.py", line 237 in dispatch 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/views/generic/base.py", line 71 in view 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/contrib/auth/decorators.py", line 22 in _wrapped_view 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/handlers/base.py", line 132 in get_response 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 189 in __call__ 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/testcases.py", line 1099 in __call__ 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/contrib/staticfiles/handlers.py", line 63 in __call__ 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 137 in run 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/servers/basehttp.py", line 182 in handle 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 673 in __init__ 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/servers/basehttp.py", line 102 in __init__ 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 344 in finish_request 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 331 in process_request 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 305 in _handle_request_noblock 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 238 in serve_forever 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/testcases.py", line 1182 in run 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 920 in _bootstrap_inner 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 888 in _bootstrap 

Current thread 0x00007fff7996a300 (most recent call first): 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 374 in readinto 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 313 in _read_status 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 351 in begin 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1171 in getresponse 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 1185 in do_open 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 1210 in http_open 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 441 in _call_chain 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 481 in _open 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 463 in open 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 457 in _request 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 389 in execute 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 191 in execute 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 447 in _execute 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 68 in click 
    File "/Users/sebcorbin/Sites/planexo/tests/test_account.py", line 203 in _test_worksite_form 
    File "/Users/sebcorbin/Sites/planexo/tests/test_account.py", line 36 in test 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 577 in run 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 625 in __call__ 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/testcases.py", line 186 in __call__ 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/suite.py", line 122 in run 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/suite.py", line 84 in __call__ 
    File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/runner.py", line 168 in run 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/runner.py", line 178 in run_suite 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/test/runner.py", line 211 in run_tests 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/commands/test.py", line 90 in handle 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/base.py", line 441 in execute 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/commands/test.py", line 74 in execute 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/base.py", line 390 in run_from_argv 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/commands/test.py", line 30 in run_from_argv 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/__init__.py", line 330 in execute 
    File "/Users/sebcorbin/.virtualenvs/planexo-py3/lib/python3.4/site-packages/django/core/management/__init__.py", line 338 in execute_from_command_line 
    File "./manage.py", line 10 in <module> 

Trả lời

5

Nhờ tất cả các bạn cho định rõ vấn đề này, tôi lấy cảm hứng bài viết này http://nedbatchelder.com/blog/201103/quick_and_dirty_multithreaded_django_dev_server.html và sửa đổi manage.py tập tin của tôi:

#!/usr/bin/env python 
from http import server 
import os 
from socketserver import ThreadingMixIn 
import sys 


def monkey_patch_test_server(): 
    # This monkey-patches HTTPServer to create a base HTTPServer class that 
    # supports multithreading 
    originalhttpserver = server.HTTPServer 

    class ThreadedHTTPServer(ThreadingMixIn, originalhttpserver): 
     def __init__(self, server_address, RequestHandlerClass=None): 
      originalhttpserver.__init__(self, server_address, 
             RequestHandlerClass) 

    server.HTTPServer = ThreadedHTTPServer 


if __name__ == "__main__": 
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planexo.settings") 

    from django.core.management import execute_from_command_line 

    if sys.argv[1] == 'test': 
     monkey_patch_test_server() 

    execute_from_command_line(sys.argv) 

Bây giờ nó hoạt động!

+0

Làm tốt lắm! Hy vọng rằng Django sẽ nhận được một máy chủ dev thích hợp vào một ngày. –

1

Sử dụng mô-đun Python faulthandler để in traceback của tất cả các chủ đề treo. Điều này sẽ xác định bạn các chức năng thực tế là treo đang xảy ra.

Nếu cần chỉnh sửa câu hỏi của bạn và thêm thông tin có liên quan.

Nếu không, tôi đoán là bạn đang sử dụng máy chủ thử nghiệm/phát triển Django theo cách mà nó chỉ có thể xử lý một yêu cầu tại một thời điểm. Yêu cầu HTTP ban đầu của bạn cho máy chủ dev kích hoạt yêu cầu yêu cầu dữ liệu từ chính nó, mà máy chủ devango Django không thể thực hiện. Nhưng đây chỉ là một phỏng đoán.

Mã nguồn có liên quan còn thiếu trong câu hỏi.

+0

Tôi đã cập nhật câu hỏi của tôi với đề xuất của bạn, nhưng không có cách nào để giải thích nó, cần có 3 chủ đề ... – SebCorbin

+1

Yeah tôi nghĩ rằng Django dev server ca nnot phục vụ chính nó: http://stackoverflow.com/a/10636757/315168 - Tôi đề nghị bạn chỉ cần quay lên một máy chủ dev khác trong một cổng localhost và sử dụng nó như là mục tiêu cho 'base_url' –

+0

Làm cho base_url cấu hình trong cài đặt. biến môi trường py hoặc OS. Sau đó, bắt đầu một cá thể máy chủ Django dev cụ thể 'python manage.py runserver 127.0.0.0: anotherport' và để cho nó phục vụ weasyprint trong chạy phát triển cục bộ. –

0

Sử dụng mô-đun faulthandler. Có lẽ nó cũng có thể được rằng Django, chỉ có thể mất 1 yêu cầu.

1

tôi sẽ chia sẻ trường hợp của tôi:

Tôi có vấn đề này với các bài kiểm tra selen Django của tôi (django == 1.7.12 và selen == 2.53.1) với ChromeDriver 2.21.371459 và Chrome 48.0.2564.116 Google .

Tôi đã có thể cách ly sự cố. Trong trường hợp của tôi, nó chỉ xảy ra cho các trang tham chiếu một tệp tĩnh (một hình ảnh trong một thẻ HTML <img> ví dụ http://cdn.local.myproject.net/static/myimage.png) trên miền cdn cục bộ tùy chỉnh của tôi. Vấn đề không có mặt nếu tôi sử dụng đường dẫn tương đối "/ static/myimage.png" hoặc localhost "http://127.0.0.1/static/myimage.png" vì vậy tôi figured nó là một vấn đề hoàn toàn DNS

tôi đã có thể bỏ qua các vấn đề bằng cách sử dụng --dns-prefetch-disable tùy chọn chrome

Ví dụ bằng Python:..

from selenium.webdriver import Chrome 
from selenium.webdriver.chrome.options import Options 

chrome_options = Options() 
chrome_options.add_argument('--dns-prefetch-disable') 
driver = Chrome(chrome_options=options) 

Tôi không biết đây có phải là trường hợp chung hay không nhưng hy vọng nó có thể giúp một số bạn

Các vấn đề liên quan