Unexpected Output:

Screen Link: https://app.dataquest.io/m/169/i%2Fo-bound-programs/4/parallel-execution

My Code:

import threading

teams = ['BSN','CHN', 'CN2', 'PT1', 'SL4', 'NY1', 'PHI','BR3', 'PIT', 'BRO', 'CIN', 'SLN', 'BLA', 'BOS', 
    'CHA', 'CLE', 'DET', 'MLA', 'PHA','WS1', 'SLA', 'NYA', 'ML1', 'BAL', 'KC1', 'LAN', 'SFN', 'LAA', 'MIN', 'WS2', 
    'HOU', 'NYN', 'CAL', 'ATL', 'OAK', 'KCA', 'SE1', 'MON', 'SDN', 'ML4', 'TEX', 'SEA', 'TOR', 'COL',
]


def task(team):
    print(team)

for i, team in enumerate(teams):
    thread = threading.Thread(target=task, args=(team,))
    thread.start()
    print("Started task {}".format(i))

Expected output:

Actual Ouput:

When I run the above code in DQ editor, it gives me the result same as the first image. But when running locally, I don’t get the expected output.

What’s going on?


Update:

If I run the above code using Python 2, then the output resembles the first image.

After doing some research I found that the print() function wasn’t thread-safe in Python 2, but it is in Python 3.

Am I correct? I’m just looking for a confirmation that I am on the right track.