3

I want to move my methods (method_2 and method_3) from the main.py file to other python files (such as method_2.py and method_3.py) and call them from there.

I have an issue that these two functions need uasyncio and uasyncio.asyn modules which is also requiring for method_1 still inside main.py. If I add these modules in each file (method_2.py and method_3.py), Will it not cause multiple inheritance when i call them from main.py? Because main.py has already used these modules (uasyncio and uasyncio.asyn).

main.py;

import uasyncio as asyncio
import uasyncio.asyn as asyn

loop = asyncio.get_event_loop()

async def handle_client(reader, writer):
    loop.create_task(asyn.Cancellable(method_1)())

    loop.create_task(asyn.Cancellable(method_2)())

    loop.create_task(asyn.Cancellable(method_3)())

@asyn.cancellable
async def method_1():
    print('method_1 is running')

# i want to move this function to another class or py file (for ex: method_2.py) and call it from there
@asyn.cancellable
async def method_2():
    print('method_2 is running')

# i want to move this function to another class or py file (for ex: method_3.py) and call it from there
@asyn.cancellable
async def method_3():
    print('method_3 is running')

loop.create_task(asyncio.start_server(handle_client, ipAddress, 5700))
loop.run_forever()

method_2.py;

import uasyncio as asyncio
import uasyncio.asyn as asyn

@asyn.cancellable
async def method_2():
    print('method_2 is running')

method_3.py;

import uasyncio as asyncio
import uasyncio.asyn as asyn

@asyn.cancellable
async def method_3():
    print('method_3 is running')

Revised_main.py (which i have considered);

import uasyncio as asyncio
import uasyncio.asyn as asyn

loop = asyncio.get_event_loop()

async def handle_client(reader, writer):
    loop.create_task(asyn.Cancellable(method_1)())

    import method_2
    loop.create_task(asyn.Cancellable(method_2.method_2)())

    import method_3
    loop.create_task(asyn.Cancellable(method_3.method_3)())

@asyn.cancellable
async def method_1():
    print('method_1 is running')

loop.create_task(asyncio.start_server(handle_client, ipAddress, 5700))
loop.run_forever()
Nemo
  • 2,441
  • 2
  • 29
  • 63
Sunrise17
  • 379
  • 3
  • 18

1 Answers1

1

Will it not cause multiple inheritance when i call them from main.py?

It will not, because import is not inheritance. Inheritance in Python looks like this:

class Child(Parent):
    # ...

What you're doing is fine and normal, you can import modules in as many Python files as you like, so long as the import dependencies are not circular (e.g. it would be bad if A imports B which imports A).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436