LINUX.ORG.RU

История изменений

Исправление vvn_black, (текущая версия) :

Что-то на меня нашло и схематично накидал на корутинах, как управлять Василием:

class SomeObject(object):
    def __init__(self):
        self._queue = asyncio.Queue()

    @property
    def queue(self):
        return self._queue

    async def process(self):
        while True:
            event = await self._queue.get()

            if event == 'sleep':
                self._sleep()

            if event == 'go_to_bank':
                self._go_to_bank()

            if event == 'killed':
                break

    def _sleep(self):

    def _go_to_bank(self):

    ...



class Scheduler(object):

    def register(self, queue):
        self._pool.append(queue)

    async def process(self):
        while True:

            if self._date.hour == 0:
                for q in self._pool:
                    await q.put('sleep')

            if (self._date.hour == 8) and (self._date.day == 20):
                for q in self._pool:
                    await q.put('go_to_bank')



loop = asyncio.get_event_loop()

vasyan = SomeObject()
sch = Scheduler()
sch.register(vasyan.queue)

tasks = [asyncio.ensure_future(vasyan.process()),
         asyncio.ensure_future(sch.process())]

loop.run_until_complete(asyncio.wait(tasks))

Исправление vvn_black, :

Что-то на меня нашло и схематично накидал на корутинах, как управлять Василием:

class SomeObject(object):
    def __init__(self):
        self._queue = asyncio.Queue()

    @property
    def queue(self):
        return self._queue

    async def process(self):
        while True:
            event = await self._queue.get()

            if event == 'sleep':
                self._sleep()

            if event == 'go_to_bank':
                self._go_to_bank()

            if event == 'killed':
                break

    def _sleep():

    def _go_to_bank()

    ...



class Scheduler(object):

    def register(self, queue):
        self._pool.append(queue)

    async def process(self):
        while True:

            if self._date.hour == 0:
                for q in self._pool:
                    await q.put('sleep')

            if (self._date.hour == 8) and (self._date.day == 20):
                for q in self._pool:
                    await q.put('go_to_bank')



loop = asyncio.get_event_loop()

vasyan = SomeObject()
sch = Scheduler()
sch.register(vasyan.queue)

tasks = [asyncio.ensure_future(vasyan.process()),
         asyncio.ensure_future(sch.process())]

loop.run_until_complete(asyncio.wait(tasks))

Исходная версия vvn_black, :

Что-то на меня нашло и схематично накидал на корутинах, как управлять Василием:

class SomeObject(object):
    def __init__(self):
        self._queue = asyncio.Queue()

    @property
    def queue(self):
        return self._queue

    async def process(self):
        while True:
            event = await self._queue.get()

            if event == 'sleep':
                self._sleep()

            if event == 'go_to_bank':
                self._go_to_bank()

    def _sleep():

    def _go_to_bank()

    ...



class Scheduler(object):

    def register(self, queue):
        self._pool.append(queue)

    async def process(self):
        while True:

            if self._date.hour == 0:
                for q in self._pool:
                    await q.put('sleep')

            if (self._date.hour == 8) and (self._date.day == 20):
                for q in self._pool:
                    await q.put('go_to_bank')



loop = asyncio.get_event_loop()

vasyan = SomeObject()
sch = Scheduler()
sch.register(vasyan.queue)

tasks = [asyncio.ensure_future(vasyan.process()),
         asyncio.ensure_future(sch.process())]

loop.run_until_complete(asyncio.wait(tasks))