ryuzak1
ryuzak1

Reputation: 234

Python Multi-Threading use data from a thread in another thread

I'm new to Python threading and what I'm trying to do is :

So I was looking for a way to start the first Thread then only start the second after the first API call and then share these data to the second Thread so it can execute its logic.

Anyone can help me pls ? Thanks.

Upvotes: 1

Views: 298

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 71707

As per you requirements, here is a simple boilerplate code you might want to try,

import time
import threading

available = False

def thread1():
    global available
    while True:
        # TODO: call API
        # --------------

        available = True # set available True after API call
        time.sleep(5) # perform API calls after every 5 seconds

def thread2():
    while True:
        # TODO: perform ping
        # --------------

        # perform ping request after every 5 seconds
        time.sleep(5)

if __name__ == "__main__":
    t1 = threading.Thread(target=thread1, name="thread1")
    t2 = threading.Thread(target=thread2, name="thread2")

    t1.start()

    while not available:
        time.sleep(0.1)
    else:
        t2.start()

Upvotes: 1

Related Questions