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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…