简体   繁体   中英

Python Threading functions from different files

I created a python file that creates a thread calling functions from two different files

import objDetection_heartbeat
import combindedsensors
from threading import Thread

threads = []

sensors_thread = Thread(name="Sensors", target=combinedsensors.ping)
threads.append(sensors_thread)
sensors_thread.start()

heartbeat_thread = Thread(name="Heartbeat", 
target=objDetection_heartbeat.heartbeat_send)
threads.append(heartbeat_thread)
heartbeat_thread.start()

heartbeat_send function is sending out a message every 5 seconds. combinedsensors.ping calculates the distance between two objects.

The python thread file I created only calls the heartbeat function. I see that coming through every 5 seconds, but I dont know why it isn't calling sensor_thread. It seems I can run one or the other, but not both. The reason I'm creating a thread is because the heartbeat is on an interval and instead of having to wait 5 seconds, I was attempting to call the sensor function in parallel with the heartbeat.

Python threads are green threads, meaning that they "simulate" real thread. In fact you have a single thread available that seemlessly jumps from a piece of code to another. CPU bound code will not benefit from Python threads capability.

I found this question that provides detailed explanations: Green-threads and thread in Python

The following might be a better solution:

yield from asyncio.sleep(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM