X Tutup
Skip to content

Coroutine getting lost after sleep #12

@brenns10

Description

@brenns10

I'm working on a plugin with a GeneratorQueryHandler which yields one item as a result, and then sleeps for a bit, and then fetches a result from an API and yields those items as well. So basically, it yields two chunks:

  1. "Web search"
  2. First 5 results of the suggestions

But I've noticed that the coroutine seems to just stop getting called sometimes. I was able to boil it down to a minimal reproducer:

import time

from albert import *

md_iid = "5.0"
md_name = "chunktest"
md_description = "testing multiple chunks of items"
md_authors = ["@brenns10"]


class Plugin(PluginInstance, GeneratorQueryHandler):

    def __init__(self):
        PluginInstance.__init__(self)
        GeneratorQueryHandler.__init__(self)

    def defaultTrigger(self):
        return "chunktest "

    def items(self, ctx):
        for i in range(5):
            if not ctx.isValid:
                info("exiting early")
                return
            info(f"yielding item {i}")
            yield [
                StandardItem(
                    id=f"item{i}",
                    text=f"Item {i}",
                    subtext="details",
                    actions=[
                        Action(
                            id="act",
                            text="Open Google",
                            callable=lambda: runTerminal("echo hi"),
                        )
                    ],
                )
            ]
            time.sleep(0.1)
            info("finished sleeping")
        info("done")

When I load this plugin, and then type "chunktest asdf" (type really fast), I only get "Item 0", and the logs look like this:

17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [warn:albert.wbm] Item retured null icon: python.chunktest.item0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] finished sleeping
17:25:52 [info:albert.python.chunktest] yielding item 1
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:52 [info:albert.python.chunktest] yielding item 0
17:25:53 [info:albert.python.chunktest] yielding item 0
17:25:53 [info:albert.python.chunktest] yielding item 0

The remaining 4 items never get populated. Here is a video that shows what I mean.

However, if you go ahead and move the time.sleep() call just before the yield statement, rather than after it, you'll get the behavior I expect. That is, the results keep getting replaced each time I type a new keystroke, and they slowly populate whenever I pause. Here's the video of this behavior.

It seems to me like there's something going on with this time.sleep() call directly after the yield. I notice I never see the "exiting early" message logged, which may or may not be related.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup