Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

Python Asyncio gather() doesn't seem to run

I have a bit of code where I want to import and run a module on the fly. Each 'plugin' is required to have a main.py with a Main() class and a main() coroutine. Here's my code so far:

core.py

class Core():

    def __init__(self) -> None:
        super().__init__()
        # some __init__ stuff here
        print('core initialized')

    async def main(self):
        enabled_processes = []
        while True:
            for i in self.env['processes']:
                if self.env['processes'][i] == 'True' and i not in enabled_processes:
                    print(i)
                    temp = self.importlib.import_module('processes.' + i + '.main')
                    temp2 = temp.Main()
                    send, receive, event = await temp2.get_queues()
                    self.processes[str(i)] = {'send': send, 'receive': receive, 'event': event}
                    enabled_processes.append(i)
                    temp_future = self.asyncio.gather(temp2.main())

processes.plugin.main.py

class Main():

    def __init__(self) -> None:
        super().__init__()
        import asyncio

        self.asyncio = asyncio

        self.send_queue = self.asyncio.PriorityQueue()
        self.receive_queue = self.asyncio.PriorityQueue()
        self.event = asyncio.Event()

        print('Plugin imported!')

    async def main(self):
        while True:
            print('main() started!')
            await self.event.wait()
            print('The event happened!')
            self.event.clear()
            await self.asyncio.sleep(1)

    async def get_queues(self):
        return self.receive_queue, self.send_queue, self.event

I expect main() to run and 'main() started!' to be outputted but the last thing this code outputs is 'Plugin imported!' from the initialization of the class. How should I be doing this? Why isn't this working as expected? Could you please explain why this doesn't work so I can not make this mistake again in the future.

question from:https://stackoverflow.com/questions/65862716/python-asyncio-gather-doesnt-seem-to-run

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...