Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6027295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:34:17+00:00 2026-05-23T04:34:17+00:00

from threading import Thread import time print ‘start of script’ class MyThread(Thread): def run(self):

  • 0
from threading import Thread
import time
print 'start of script'

class MyThread(Thread):
    def run(self):
        for i in xrange(10):
            print 'thread->', '['+self.name+']', '('+str(i)+')'
            time.sleep(2)


for i in range(3):
    my_thread = MyThread()
    my_thread.name = i
    my_thread.start()

print 'end of script'

>>> ================================ RESTART ================================
>>> 
start of script
thread->thread->thread->end of script   
[0][1][2]
>>>    (0)(0)(0)


thread->thread->thread->   [0][2][1]   (1)(1)(1)


thread->thread-> thread-> [0] [2] [1] (2) (2)
(2)

thread-> thread->thread->[0]   [2][1](3)  
(3)(3)

thread-> thread->thread->[0]   [2][1](4)  
(4)(4)

thread-> thread->[0]thread->   [2](5)[1] 
 (5)(5)

thread-> [0]thread->thread->   (6)[2][1]
  (6)(6)

thread-> thread->[0]thread->   [2](7)[1] 
 (7)(7)

thread-> thread->[0] thread-> [2] (8) [1]
(8) 
(8)
thread->thread-> thread-> [0] [2] [1] (9) (9)
(9)


>>> 

As you can see I’m printing ‘start of script’ first, then executing multiple threads, and then printing ‘end of script’

Yet, ‘end of script’ gets printed right after I execute the first thread, instead of after all the threads have completed. How can I prevent this?

The jumbled nature of the output is expected and in fact desired as these threads are supposed to be executing simultaneously…

I’m on Windows 7 python 2.7 btw…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:34:18+00:00Added an answer on May 23, 2026 at 4:34 am

    You want to add .join(), since by default there’s no reason for your main program to block until the threads are finished:

    my_threads = []
    for i in range(3):
        my_thread = MyThread()
        my_thread.name = i
        my_thread.start()
        my_threads.append(my_thread)
    
    for t in my_threads:
        t.join()
    
    print 'end of script'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: from threading import Thread import time class Timer(Thread): def __init__(self,
I'm running this simple code: import threading, time class reqthread(threading.Thread): def run(self): for i
import os import sys, urllib2, urllib import re import time from threading import Thread
From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread.
Why doesn't this code act threaded? (Please see the output.) import time from threading
I'm having a bit of trouble with this queue: import Queue import threading class
The doBackgroundWork method does not get called from the startDoingWork method. threading.h ------------ #import
From the MSDN article on STAThread: Indicates that the COM threading model for an
Both System.Timers.Timer and System.Threading.Timer fire at intervals that are considerable different from the requested
From time to time I see an enum like the following: [Flags] public enum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.