r/MicroPythonDev • • Jun 26 '24

how to run 2 processes parallelly

Hello. This is Automatic key box project. I am trying to have 2 processes running simultaneously and continuously. One is handling Username/Password Loggin. The other is listening to reset command. I have been trying Asynchronous function, but in vain. Per attached screenshot below, process A is working fine, while process B has not even started.

Hardware is ESP32-WROOM-32. Appreciate your advice please.

3 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/joey_speedy Jun 26 '24

Thanks a lot..chanamasala4life

Per your great advice, I revised the code and tested. Look like I now (just now) understand that async/await function actually not running each of the tasks parallelly. They just actually take turns to run, and that taking turn is managed by await function. Since my process A would be stuck waiting for customer login (maybe like once in a while), then my Process B will never get to work till then.

Apologized. I just start programming adventure. Learning curve is so steep. I will study further. Thanks again.

main.py
from machine import Pin

from time import sleep

from verification import Password

import asyncio

key201 = Pin(4 , Pin.OUT)

key202 = Pin(26, Pin.OUT)

key203 = Pin(27, Pin.OUT)

key204 = Pin(16, Pin.OUT)

key205 = Pin(17, Pin.OUT)

key206 = Pin(18, Pin.OUT)

key207 = Pin(19, Pin.OUT)

key208 = Pin(23, Pin.OUT)

key209 = Pin(25, Pin.OUT)

reset = Pin(35, Pin.IN, Pin.PULL_UP)

keylist = {"201":key201, "202":key202, "203":key203, "204":key204, "205":key205, "206":key206, "207":key207, "208":key208, "209":key209,}

async def execution():

while True:

Pass_check = Password()

relay2act = Pass_check.check()

keylist[relay2act].value(1)

await asyncio.sleep(3)

async def resetting():

while True:

print("hello")

await asyncio.sleep(1)

while True:

if reset.value() == 1:

for x in keylist:

keylist[x].value(0)

await asyncio.sleep(1)

main=asyncio.gather(execution(), resetting())

asyncio.run(main)

2

u/chanamasala4life Jun 26 '24

No worries, keep at it. It gets more fun the more you know!