So, I've been programming with Python for a while, but I am by no means near proficient. I have also been playing with pygame, and while doing this, I came across a problem that I could get around through other methods (see below for more), but I would like to know if there is a way to have multiple python scripts running at the same time and interacting together. This is what I mean.
I have programmed with Arduino boards before, and when programming those, you can have multiple tasks that you define, kind of like functions. In your main task, you can start a separate task that runs in the background. For example, here is some pseudocode.
Define task Emergency_Stop:
If the emergency button is pressed, then stop all motors.
Set x = False
start main task:
while(x==True):
start_task(Emergency_Stop)
Turn on all motors
This code would start all the motors with the main task, but if, at any time the emergency button is pressed, it will stop all motors because that separate task was running in the background. So what I'm wondering is if there is a way to do something similar with python, allowing me to run my main script while at the same time, running a separate script that interacts with variables in the main script, and if I were to run time.sleep(3000), for example, in the separate script, it would not pause the main script.
Here is an example of what I want to do, and a way to get around it.
import pygame, time
#I'm not entirely sure how to make a timer yet, but let's say t equals a timer of sorts
t=Timer
def mouse_click():
#If the mouse has been clicked and the timer is greater than 5 seconds
if pygame.mouse.get_pressed[0] and t > 5:
#Set the timer back to 0 and print "Your mouse has been clicked"
t = 0
print "Your mouse has been clicked"
def main():
while(True):
mouse_click()
main()
This would allow me to run through all of my main loop with out being interrupted if there was more in my main loop to run through. What I want to be able to do, though, is instead of have a timer, I would have the mouse_click() function wait for 5 seconds after the mouse has been pressed. I can't do this though, without causing my whole program to pause for five seconds. I think I need to use threaders, after doing some research, but I have no experience with threaders, and I haven't found anything that makes sense as to how to use them. If someone could direct me to a good place to learn, that would be great. Thank you!