Python Forum
Watching for inputs from multiple sources concurrently
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Watching for inputs from multiple sources concurrently
#5
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 https://pymotw.com/3/multiprocessing/basics.html
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:
            print(ctr)
            ctr += 1
            time.sleep(sleep_time)

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

if __name__ == '__main__':
     ## run function in the background
     CT=TestClass()

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

     CT.tk_quit()
     print("terminating processes")
     for p in [p1, p2]:
         if p.is_alive():
             p.terminate()
         p.join() 
Reply


Messages In This Thread
RE: Watching for inputs from multiple sources concurrently - by woooee - Mar-03-2019, 07:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple variable inputs when only one is called for ChrisDall 2 488 Oct-20-2023, 07:43 PM
Last Post: deanhystad
Lightbulb Multiple inputs on the same line (beginner) dementshuk 9 2,776 Sep-03-2021, 02:21 PM
Last Post: dementshuk
  Generate Multiple sql Files With csv inputs vkomarag 13 4,200 Aug-20-2021, 07:03 PM
Last Post: vkomarag
  Help with applying this instrument monitoring code to multiple inputs. Kid_Meier 1 2,089 Mar-04-2020, 12:01 PM
Last Post: Kid_Meier
  Problem with accepting multiple string inputs Ryan_Todd 5 2,928 Jan-22-2020, 06:12 PM
Last Post: buran
  reading 2 files concurrently Skaperen 3 3,218 Jun-01-2018, 07:07 AM
Last Post: Skaperen
  multiple inputs newbee 3 3,203 May-20-2018, 01:20 AM
Last Post: snippsat
  Python - Make Json objects to work concurrently through Threads? WeInThis 0 2,605 Sep-22-2017, 11:31 AM
Last Post: WeInThis
  run two tasks concurrently tony1812 1 2,602 Jul-24-2017, 05:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020