Hi

I want to execute at least two python files at once when imported but I dont 
know how to do this. Currently I can only import each file one after another 
but what i want is each file to be imported at the same time. Can you help me 
write the code for this? embedded.py is the main file to execute.

embedded.py
import paho.mqtt.client as mqtt
from mqtt2 import *
import os
import time
import json
import configparser
from threading import Thread

def start():
    try:
        os.remove("list_of_device(s)_currently_active.txt")
        os.remove("laser.ini")
        print("Awaiting device(s) to be activated")
    except:
        print("Awaiting device(s) to be activated")
start()
        
devices = list(map(str,input("Device(s) to be activated: ").split(",")))

client = embedded()
client.run()

client.loop_start()
print("Connected to broker")
time.sleep(1)
print("Subscribing to topic", "microscope/light_sheet_microscope/UI")
client.subscribe("microscope/light_sheet_microscope/UI")
print("Publishing message to topic", "microscope/light_sheet_microscope/UI")
client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": 
"system", "payload":{"name": devices, "cmd": "activating device(s)"}}, 
indent=2))
time.sleep(1)

def active_devices():
    for item in devices:
        device = (item + "Embedded")
        deviceImport = __import__(device)

    with open("list_of_device(s)_currently_active.txt", "a+") as myfile:
        for item in devices:
            myfile.write(item + "\n")
active_devices()

def readFile(fname):    
    print("List of device(s) currently active:")
    try:
        with open(fname, "r") as f:
            for item in f:
                print(item.rstrip("\n"))
    except:
        print("No device(s) added yet")
readFile("list_of_device(s)_currently_active.txt")

# print("Connected to broker")
# time.sleep(1)
# client.subscribe("microscope/light_sheet_microscope/UI/laser/#")
# if os.path.exists:
#     parser = configparser.ConfigParser()
#     parser.read("laser.ini")

#     try:
#         subscriptions = dict(parser.items("Subscriptions"))
#         print("Subscribing to topics", subscriptions)
#         client.subscribe(subscriptions)
#     except:
#         pass
# else:
#     pass

client.loop_forever()

laserEmbedded.py
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
from mqtt2 import *

class Laser(Actor):
    async def handle_message(self, message: Message):
        print("Laser")
        await asyncio.sleep(2)
        print("Unitialised")
        await asyncio.sleep(2)
        print("Initialising")
        await asyncio.sleep(2)
        print("Initialised")
        await asyncio.sleep(2)
        print("Configuring")
        await asyncio.sleep(2)
        print("Configured")
        await asyncio.sleep(2)
        await message.sender.tell(DataMessage(data="Hello World Im a laser!" + 
"\n", sender=self))
async def main():
    # Let's create an instance of a Greeter actor and start it. 
    async with Laser() as laser:
        # Then we'll just send it an empty message and wait for a response
        reply : DataMessage = await ask(laser, Message())
    print(reply.data)
asyncio.get_event_loop().run_until_complete(main())

def subscribe(): 
    client = embedded()
    client.run()

    client.loop_start()
    client.subscribe("microscope/light_sheet_microscope/UI/laser/#")
subscribe()

camerasEmbedded.py
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference

class Cameras(Actor):
    async def handle_message(self, message: Message):
        print("Cameras")
        await asyncio.sleep(2)
        print("Unitialised")
        await asyncio.sleep(2)
        print("Initialising")
        await asyncio.sleep(2)
        print("Initialised")
        await asyncio.sleep(2)
        print("Configuring")
        await asyncio.sleep(2)
        print("Configured")
        await asyncio.sleep(2)
        await message.sender.tell(DataMessage(data="Hello World Im a camera!" + 
"\n", sender=self))
async def main():
    # Let's create an instance of a Greeter actor and start it. 
    async with Cameras() as cameras:
        # Then we'll just send it an empty message and wait for a response
        reply : DataMessage = await ask(cameras, Message())
    print(reply.data)
asyncio.get_event_loop().run_until_complete(main())

filterwheelEmbedded.py
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference

class FW(Actor):
    async def handle_message(self, message: Message):
        print("Filter wheel")
        await asyncio.sleep(2)
        print("Unitialised")
        await asyncio.sleep(2)
        print("Initialising")
        await asyncio.sleep(2)
        print("Initialised")
        await asyncio.sleep(2)
        print("Configuring")
        await asyncio.sleep(2)
        print("Configured")
        await asyncio.sleep(2)
        await message.sender.tell(DataMessage(data="Hello World Im a filter 
wheel!" + "\n", sender=self))
async def main():
    # Let's create an instance of a Greeter actor and start it. 
    async with FW() as fw:
        # Then we'll just send it an empty message and wait for a response
        reply : DataMessage = await ask(fw, Message())
    print(reply.data)
asyncio.get_event_loop().run_until_complete(main())

motorized_galvo_wheelEmbedded.py
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference

class Motorized_galvo_wheel(Actor):
    async def handle_message(self, message: Message):
        print("Motorized galvo wheel")
        await asyncio.sleep(2)
        print("Unitialised")
        await asyncio.sleep(2)
        print("Initialising")
        await asyncio.sleep(2)
        print("Initialised")
        await asyncio.sleep(2)
        print("Configuring")
        await asyncio.sleep(2)
        print("Configured")
        await asyncio.sleep(2)
        await message.sender.tell(DataMessage(data="Hello World Im a motorized 
galvo wheel!" + "\n", sender=self))
async def main():
    # Let's create an instance of a Greeter actor and start it. 
    async with Motorized_galvo_wheel() as motorized_galvo_wheel:
        # Then we'll just send it an empty message and wait for a response
        reply : DataMessage = await ask(motorized_galvo_wheel, Message())
    print(reply.data)
asyncio.get_event_loop().run_until_complete(main())

stagesEmbedded.py
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference

class Stages(Actor):
    async def handle_message(self, message: Message):
        print("Stages")
        await asyncio.sleep(2)
        print("Unitialised")
        await asyncio.sleep(2)
        print("Initialising")
        await asyncio.sleep(2)
        print("Initialised")
        await asyncio.sleep(2)
        print("Configuring")
        await asyncio.sleep(2)
        print("Configured")
        await asyncio.sleep(2)
        await message.sender.tell(DataMessage(data="Hello World Im a stage!" + 
"\n", sender=self))
async def main():
    # Let's create an instance of a Greeter actor and start it. 
    async with Stages() as stages:
        # Then we'll just send it an empty message and wait for a response
        reply : DataMessage = await ask(stages, Message())
    print(reply.data)
asyncio.get_event_loop().run_until_complete(main())     

Thanks
Spencer
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to