Git Product home page Git Product logo

pawelmhm.github.io's People

Contributors

pawelmhm avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pawelmhm.github.io's Issues

Aiohttp limits the number of simultaneous connections

Thanks for your post on aiohttp - https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html

When I tried to run the client/server code locally to benchmark I noticed the server was receiving connections in batches of 100 approx. This seriously limits the performance.
I found this answer on stackoverflow on why it's happening - https://stackoverflow.com/a/43857526
The script in blog can be modified to following to fix this

# modified fetch function with semaphore
import asyncio
from aiohttp import TCPConnector, ClientSession

async def fetch(url, session):
    async with session.get(url) as response:
        delay = response.headers.get("DELAY")
        date = response.headers.get("DATE")
        print("{}:{} with delay {}".format(date, response.url, delay))
        return await response.text()


async def bound_fetch(sem, url, session):
    # Getter function with semaphore.
    async with sem:
        await fetch(url, session)


async def run(r):
    url = "http://localhost:8000/{}"
    tasks = []
    # create instance of Semaphore
    sem = asyncio.Semaphore(1000)

    # Create client session that will ensure we dont open new connection
    # per each request.
    conn = TCPConnector(limit=0)
    async with ClientSession(connector=conn) as session:
        for i in range(r):
            # pass Semaphore and session to every GET request
            task = asyncio.ensure_future(bound_fetch(sem, url.format(i), session))
            tasks.append(task)

        responses = asyncio.gather(*tasks)
        await responses

number = 10000
asyncio.run(run(number))

Consider using go-httpbin instead of httpbin

Hey, dude, thank you for your post about 1m requests.

I really liked it, and it helped me much. But I guess there is a need to add go-httpbin instead of default httpbin. The default one was giving me ServerDisconnected && ClientConnector errors. Also, I think, it's an option to add ulimit -n ${SIZE_OF_SEMAPHORE} and use aiohttp.TCPConnector(limit=SIZE_OF_SEMAPHORE)
which would definitely increase performance.

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.