Giter VIP home page Giter VIP logo

Comments (3)

sepulworld avatar sepulworld commented on May 22, 2024

Example of what I am looking to do since I believe req.media requires await.

@api.route("/dinghy/ping/domains")
async def ping_multiple_domains(req, resp):
    """
    Async process to test multiple domains and return JSON with results
    Post request data example
    {
      "domains": [
        {
          "protocol": "https",
          "domain": "google.com"
        },
        {
          "protocol": "https",
          "domain": "microsoft.com"
        }
      ]
    }

    Return results
    {
      "domains": [
        {
          "protocol": "https",
          "domain": "google.com",
          "domain_response_code": "200",
          "domain_response_text": "text",
          "domain_response_time_ms": "30.0ms"
          " 
        },
        {
          "protocol": "https",
          "domain": "microsoft.com"
          "domain_response_code": "200",
          "domain_response_text": "text",
          "domain_response_time_ms": "200.1ms"
        }
      ]
    }
    """

    results = {"domains": []}
    loop = asyncio.get_event_loop()

    def build_domain_results(protocol, request_domain):
        domain_results = _process_request(protocol, request_domain) 
        results['domains'].append = {
            "protocol": protocol,
            "domain": request_domain,
            "domain_response_code": domain_results[0],
            "domain_response_text": domain_results[1],
            "domain_response_time_ms": domain_results[2]
        }
        esults['domains'].append(domain_results)
    
    def gather_results(data): 
        for domain in data['domains']:
            protocol = domain['protocol']
            request_domain = domain['domain']
            build_domain_results(protocol, request_domain)

    tasks = [
        asyncio.ensure_future(req.media),
        asyncio.ensure_future(gather_results(data=req.media()))
    ]

    loop.run_until_complete(asyncio.gather(*tasks))

    resp.media = results

However, when doing this I run into multiple event loops

RuntimeError: this event loop is already running.

Or is there a different approach to accessing POST data where I can avoid using async?

from responder.

sepulworld avatar sepulworld commented on May 22, 2024

After reviewing the test suite looks like POST do require async. I think I understand why as well.

I was able to work things out, not sure if it is the 'best' way to do it though.

@api.route("/dinghy/ping/domains")
async def ping_multiple_domains(req, resp):

    results = {"domains": []}

    def build_domain_results(protocol, request_domain, results):
        domain_response_code, domain_response_text, domain_response_time_ms = _process_request(protocol, request_domain) 
        results['domains'].append({ 
            "protocol": protocol,
            "domain": request_domain,
            "domain_response_code": domain_response_code,
            "domain_response_time_ms": domain_response_time_ms
        })
    
    def gather_results(data): 
        for domain in data['domains']:
            protocol = domain['protocol']
            request_domain = domain['domain']
            build_domain_results(protocol, request_domain, results)

    resp.media = {"domains": results, "wait": gather_results(await req.media())}

from responder.

kennethreitz avatar kennethreitz commented on May 22, 2024

that is correct

from responder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.