Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Need example for working with shared mutable states. #53

@kvinwang

Description

@kvinwang

I was trying to make a supervisor app with async/await and failed.
I can express my thoughts in Python as following code, but i don't know how to do it in Rust.

import asyncio


class App:
    def __init__(self):
        self.api = ControlService(self)
        self.business = MyBussinessService()

    async def serve(self):
        await asyncio.wait([
            self.api.serve(),
            self.business.serve()
        ])


class ControlService:
    def __init__(self, app):
        self.app = app

    async def serve(self):
        while True:
            print('[ControlService   ] Waiting for user cmd...')
            cmd = await simulate_user_request()
            print("[ControlService   ] Received user cmd: {}".format(cmd))
            if cmd == "reset":
                # here we need to mutate the business and get the result
                ok = self.app.business.reset()
                if ok:
                    pass
                else:
                    pass
            else:
                pass


class MyBussinessService:
    def __init__(self):
        self._value = 0

    def reset(self):
        self._value = 0
        return True

    async def serve(self):
        while True:
            print('[BussinessService ] Business value: {}'.format(self._value))
            self._value += 1
            await asyncio.sleep(1)


async def simulate_user_request():
    await asyncio.sleep(5)
    return "reset"


if __name__ == '__main__':
    app = App()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(app.serve())
    loop.close()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions