Goroutines in D Programming Language

Our first program will demonstrate the concept of a goroutine, which is a lightweight thread of execution.

import time
import threading

def f(from_str):
    for i in range(3):
        print(f"{from_str} : {i}")

def main():
    # This function call runs synchronously
    f("direct")

    # Running the function in a separate thread (similar to a goroutine)
    threading.Thread(target=f, args=("thread",)).start()

    # Running an anonymous function in a thread
    threading.Thread(target=lambda msg: print(msg), args=("going",)).start()

    # Wait for the threads to finish (for a more robust approach, use join on threads)
    time.sleep(1)
    print("done")

if __name__ == "__main__":
    main()

Suppose we have a function call f(from_str). Here’s how we’d call that in the usual way, running it synchronously.

f("direct")

To invoke this function in a separate thread, use threading.Thread(target=f, args=("thread",)).start(). This new thread will execute concurrently with the calling one.

threading.Thread(target=f, args=("thread",)).start()

You can also start a thread for an anonymous function call.

threading.Thread(target=lambda msg: print(msg), args=("going",)).start()

Our two function calls are running asynchronously in separate threads now. Wait for them to finish.

time.sleep(1)
print("done")

When we run this program, we see the output of the blocking call first, then the output of the two threads. The threads’ output may be interleaved because threads are being run concurrently by the Python runtime.

$ python goroutines.py
direct : 0
direct : 1
direct : 2
thread : 0
going
thread : 1
thread : 2
done

Next we’ll look at a complement to threads in concurrent Python programs: queues.