'Python doing those two things at a time, multitasking or not?

EDIT

from manish and njzk2 suggestion:

now i recall trying to use when_motion but getting:

type error: 'NoneType' object is not callable

when put in the fonction.

Putting it outside the function like you suggest seemed a good idea, but the error still pop and interrupt .inventory() also the sound is playing instantly although no motion occurred

what i've done:

from PN5180 import PN5180
from gpiozero import MotionSensor
import vlc

player = vlc.MediaPlayer()

def play(element):
    print ("{}!".format(element))
    player.set_media(vlc.Media("{}.wav".format(element)))
    player.play()

while True:
      
      
    cards = PN5180().inventory()
    print(f"{len(cards)} card(s) detected: {' - '.join(cards)}")
    cardcount = len(cards)
    
    MotionSensor(17).when_motion(Play("feu"))

still getting error:

MotionSensor(17).when_motion(Play("feu"))

type error: 'NoneType' object is not callable

even in the simplest context:

from gpiozero import MotionSensor

While true:

    MotionSensor(17).when_motion()
    print("motion detected")

the error is return.

What does it means?


i'm currently trying to set my first python/raspberry pi project up

in order to interface a rfid reader, i'm using this library: https://github.com/fservida/pyPN5180

it consiste in one usable methode that returne a list up to 16 nfc presents chips. for what i understand in order for it to update you need to put it in a loop.

from PN5180 import PN5180

reader = PN5180

 while True:
        cards = reader.inventory() #getting the list
        print(f"{len(cards)} card(s) detected: {' - '.join(cards)}")
        time.sleep(.4)

i'd like to listen constantly for chips while also listening for movement on 3 motion sensors

but if i add my sensor function

from PN5180 import PN5180

reader = PN5180

 while True:
        cards = reader.inventory() #getting the list
        print(f"{len(cards)} card(s) detected: {' - '.join(cards)}")
        time.sleep(.4)

        mySensorFonction()

Then cards list does'nt iterate/update anymore, and putting cards on the reader doesn't trigger anything that i call

here's the sensor function just in case:

from gpiozero import MotionSensor
import vlc

player = vlc.MediaPlayer()

def PirSound(pin, element):
    
    pir = MotionSensor(pin=pin)
    pir.wait_for_motion()
    print ("{}!".format(element))
    player.set_media(vlc.Media("{}.wav".format(element)))
    player.play()
    
    

i'd like the list of detected chip updating constantly while doing other things and be able to pickup what's inside the list anytime i want to trigger events and states

doing a quick search i stumble upon the mutitasking library. is it the solution for me? how would you set this up?

hope it's clear enough ^^



Solution 1:[1]

use

def play():
    print ("{}!".format(element))
    player.set_media(vlc.Media("{}.wav".format(element)))
    player.play()

and 

pir.when_motion(play)

instead of calling wait_for_motion().

wait_for_motion is blocking (...it wait for a motion), while when_motion registers a callback that is called when a motion is detected.

Solution 2:[2]

From what I can tell, what you're looking for is multi-threading, the multi threading implementation in python is called threading, here's a nice explanation + tutorial from Geeks for Geeks link

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 njzk2
Solution 2 Piboy314