Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Watching for inputs from multiple sources concurrently
This is a question that I'm not sure how to begin answering - despite having written a few python scripts several thousand lines long, they've all been procedural - from one end to the other (spatial data manipulation), and despite some googling over the past few weeks with what I'm sure amounts to incorrect temrinology. The answer might be more conceptual than not - I'm not sure.

How would I go about watching for several inputs, each from different sources, at once?

If I'm on a raspberry pi, for example, I might like to be watching for a button press, input from another gpio source, incoming instructions from a network connection. On a PC I might wish to watch for incoming UDP packets whilst also allowing user input, etc.

Is there a particular strategy or method by which to watch for these things concurrently? Equally, is there some sort of terminology that would help me to find examples?
There is a built-in driver that will monitor several GPIO pins and trigger events when any change
Thanks Larz, though that is not quite what I'm after.

I guess what I'm asking is - when I see example code it's always setting up for a particular job. Watch for an incoming UDP packet over network. Wait for an incoming IR signal. Wait for the user to press a button. Etc. Each of these things is always given in a function with a singular purpose - makes the code easy.

But what if I want the device to both wait for a UDP packet, and also keep an eye on an IR signal and wait on the button press (or any other types of inputs / stimulus). IS this a case for round robin checking of sensors? Then I think I'll probably miss half an IR signal, or miss the UDP packet, etc. Can I have multiple functions watching all at once?... Not sure. What would be the term to research to look at that?
round robin is not good, wastes CPU time, and as you state, misses signals from time to time.
You're definitely looking for event (interrupt) driven software, but for any devise.
Something more in line with this:
Quote:I want the device to both wait for a UDP packet, and also keep an eye on an IR signal
Multiprocessing is one way to do more than one thing at a time
Quote:and wait on the button press
Keep this in the program, i.e. not a separate process, as the program controls the terminal and input. A somewhat funky example from my toolbox that uses a Tkinter Button to terminate, and hopefully will help.
import tkinter as tk
import time
from multiprocessing import Process

class TestClass():
    def test_f(self, sleep_time=0.5):
        """ a simple counter function that simulates
            a separate process 
        ctr = 0
        while True:
            ctr += 1

    def tk_quit(self):
        """ this function just waits for a button click and then
            exits/returns, allowing the rest of this program to execute
        tk.Button(root, text="Quit", command=root.quit,
                  width=10, bg="orange").grid()

if __name__ == '__main__':
     ## run function in the background

     ## simulate 2 processes running different things
     ## start as many processes as you want
     p2=Process(target=CT.test_f, args=(0.75,))

     print("terminating processes")
     for p in [p1, p2]:
         if p.is_alive():

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Capturing inputs values from internal python script limors11 11 466 Jun-16-2019, 05:05 PM
Last Post: DeaD_EyE
  user inputs in constructing a dictionary Exsul 3 246 Apr-10-2019, 12:25 PM
Last Post: ichabod801
  unit testing a method that asks two user inputs() in console gebel 0 195 Apr-03-2019, 07:59 PM
Last Post: gebel
  Infinite # of inputs Hermann_Fegelein 1 436 Sep-02-2018, 06:45 PM
Last Post: woooee
  reading 2 files concurrently Skaperen 3 602 Jun-01-2018, 07:07 AM
Last Post: Skaperen
  multiple inputs newbee 3 796 May-20-2018, 01:20 AM
Last Post: snippsat
  Regular Expressions with inputs MethodRunner147 1 896 Oct-24-2017, 07:15 PM
Last Post: Mekire
  Python - Make Json objects to work concurrently through Threads? WeInThis 0 950 Sep-22-2017, 11:31 AM
Last Post: WeInThis
  MIDI Inputs NightPython42 3 1,380 Aug-10-2017, 02:37 PM
Last Post: sparkz_alot
  run two tasks concurrently tony1812 1 712 Jul-24-2017, 05:43 PM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)