Threading: Hexiwear Example 1 Explain


#1

Good day,
With regard to Hexiwear Example 1, I noted the threading library was imported. It seems as though some aspect of the threading feature was implemented on the system but not in the conventional way through the following line of code:

thread(read_bt_status)

Could someone please explain this? I assume this means the function read_bt_status() will run every second as the main code is in sleep mode (during that 3 second sleep)?
I need this verified please. Based on my knowledge of threading (very limited) in python, I never came across this format of threading.
Thanks in advance for your assistance.
Regards,
Aaron.


Reading MAX30101 Raw Data in Hexiwear
#2

Hi @aaron, the thread() function is a Zerynth specific builtin (so no worries about you knowledge of threading in Python :slight_smile:).

We can take the following code as example:

import streams
streams.serial()

def parallel_job():
  while True:
    print("Hey from parallel_job")
    sleep(1000)

thread(parallel_job)

while True:
  print("Hey from the main thread")
  sleep(300)

the thread(parallel_job) will just call parallel_job() but in another thread, so you can see it as a concurrent execution of your code. Note: you cannot make assumptions about when different threads will be executed, it can be anytime - not only during sleeps!

In my example code, you may have some problems if both the threads will execute print() simultaneously. In fact you probably will end up mixed up strings but you can try yourself and play around with that :smile: