Skip to content

FEAT: make it easy to define generator methods in Actor #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 26, 2023

Conversation

liunux4odoo
Copy link
Contributor

What do these changes do?

Generators can not be transferred directly between actors, users must wrap it to a iterator object referencing to the code of xinference ModelActor.
But the wrapping codes are complex and need to rewrite them in every generators.

This PR provides a decorator: xo.generator, users can define generators as usual in a simple way:

import asyncio
import time
import xoscar as xo


address = "127.0.0.1:8000"

class WorkerActor(xo.StatelessActor):
    @xo.generator
    def chat(self):
        for x in "hello oscar by sync":
            yield x
            time.sleep(0.1)

    @xo.generator
    async def achat(self):
        for x in "hello oscar by async":
            yield x
            await asyncio.sleep(0.1)

    @classmethod
    def uid(cls):
        return "worker"


class SupervisorActor(xo.StatelessActor):
    @xo.generator
    async def chat(self):
        worker_actor: xo.ActorRef["WorkerActor"] = await xo.actor_ref(address=address, uid=WorkerActor.uid())
        yield "\nsync"
        async for x in await worker_actor.chat(): # this is much confused. I will suggest use async generators only.
            yield x
        
        yield "\nasync"
        async for x in await worker_actor.achat():
            yield x

    @classmethod
    def uid(cls):
        return "supervisor"


async def main():
    pool = await xo.create_actor_pool(address, 2)
    await xo.create_actor(WorkerActor, address=address, uid=WorkerActor.uid())
    superivsor_actor = await xo.create_actor(SupervisorActor, address=address, uid=SupervisorActor.uid())

    async for x in await superivsor_actor.chat():
        print(x)

    await pool.join()


asyncio.run(main())

Related issue number

Fixes #xxxx

Check code requirements

  • tests added / passed (if needed)
  • Ensure all linting tests pass

@XprobeBot XprobeBot added this to the v0.2.0 milestone Dec 23, 2023
@qinxuye qinxuye changed the title make it easy to define generator methods in Actor FEAT: make it easy to define generator methods in Actor Dec 23, 2023
Copy link

codecov bot commented Dec 25, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c7f3de4) 89.02% compared to head (c4d53a2) 88.62%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #82      +/-   ##
==========================================
- Coverage   89.02%   88.62%   -0.41%     
==========================================
  Files          48       47       -1     
  Lines        4010     3920      -90     
  Branches      764      751      -13     
==========================================
- Hits         3570     3474      -96     
- Misses        355      360       +5     
- Partials       85       86       +1     
Flag Coverage Δ
unittests 88.54% <ø> (-0.31%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codingl2k1
Copy link
Contributor

Known issue: If the caller crashes, the generator will leak.

@aresnow1 aresnow1 merged commit a442021 into xorbitsai:main Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants