Code Monkey home page Code Monkey logo

Comments (2)

davfsa avatar davfsa commented on July 17, 2024

Managed to fix this 👍

from hikari.

 avatar commented on July 17, 2024

Reproduced with optimisations introduced in b9d2855 and the following test script.

$ python -m hikari
hikari v2.0.0 HEAD
located at /home/guesswho/code/hikari/hikari
CPython 3.8.3 GCC 10.1.0
Linux z170x 5.4.52-1-MANJARO #1 SMP PREEMPT Thu Jul 16 16:07:11 UTC 2020 x86_64
import asyncio
import logging
import selectors

import hikari.impl.event_manager_base


logging.basicConfig(level="DEBUG")


class FooEvent(hikari.Event):

    @property
    def app(self) -> hikari.RESTAware:
        return NotImplemented


class Dispatcher(hikari.impl.event_manager_base.EventManagerBase):
    async def on_foo(self, _, __):
        await self.dispatch(FooEvent())


instance = Dispatcher(NotImplemented, None)


async def producer():
    i = 0
    while True:
        # Do a really short sleep, which puts an event on the selector and 
        # causes a different task to be invoked. Asyncio otherwise appears
        # to immediately invoke the same task, meaning we would keep producing
        # and never get a chance to consume. This might be a CPython bug, I
        # am not sure.

        # Note, setting to anything NON ZERO (even 0.0000000001) seems to hide
        # this bug, annoyingly.
        await asyncio.sleep(0)

        i += 1
        if not (i % 1000):
            print("produced", i)

        instance.consume_raw_event(NotImplemented, "FOO", {})


async def consumer():
    i = 0
    while True:
        i += 1
        if not (i % 1000):
            print("consumed", i)

        def pred(curry):
            def predicate(event):
                nonlocal curry
                if curry:
                    return True
                else:
                    curry = True
                    return False


        await asyncio.gather(
            instance.wait_for(FooEvent, timeout=None, predicate=pred(True)),
            instance.wait_for(FooEvent, timeout=None, predicate=pred(False)),
            instance.wait_for(FooEvent, timeout=None, predicate=pred(True)),
            instance.wait_for(FooEvent, timeout=None, predicate=pred(False)),
        )



async def runner():
    asyncio.create_task(consumer())
    asyncio.create_task(producer())


selector = selectors.SelectSelector()
asyncio.set_event_loop(asyncio.SelectorEventLoop(selector))
asyncio.get_event_loop().set_debug(True)
asyncio.get_event_loop().run_until_complete(runner())
asyncio.get_event_loop().run_forever()

from hikari.

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.