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

  • Home
  • SEARCH
  • 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 7568033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:49:16+00:00 2026-05-30T14:49:16+00:00

I am a newbie in Python. I have written following program and expecting all

  • 0

I am a newbie in Python. I have written following program and expecting all the threads to work in parallel while they are working sequentially. Can you please suggest where is the problem:

#! /usr/bin/env python3.2

# Import required libraries
import sys
import threading
import time

# Scan command line arguments
noOfThreads     = int( sys.argv[1] )

# Initialize an array for storing thread IDs
threadList = [ None ] * ( noOfThreads + 1 )

# Define a class Fast which implements threading
class Fast( threading.Thread ):
    def __init__( self, threadId ):
        self.threadID = threadId

    def run( self ):
        print( "P(" + str( self.threadID ) + ") sleeping" )
        time.sleep(5)
        print( "P(" + str( self.threadID ) + ") entering CS" )
        # CS
        print( "P(" + str( self.threadID ) + ") exiting CS" )

# Initiate and run the threads
for thrId in range( 1, noOfThreads + 1 ):
    threadList[ thrId ] = Fast( thrId )
    threadList[ thrId ].run()

O/P of the program when run with with 3 threads:

P(1) sleeping
P(1) entering CS
P(1) exiting CS
P(2) sleeping
P(2) entering CS
P(2) exiting CS
P(3) sleeping
P(3) entering CS
P(3) exiting CS
  • 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-30T14:49:17+00:00Added an answer on May 30, 2026 at 2:49 pm
    • to start a thread call its .start() method. Never call .run() explicitly
    • you forgot to call parent .__init__() method

    You don’t need to create a Thread subclass to use threads; a simple function will do:

    #!/usr/bin/env python3
    import sys
    import time
    from threading import Thread
    
    def task(tid):
        print("P({}) sleeping".format(tid))
        time.sleep(5)
        print("P({}) entering CS".format(tid))
        # CS
        print("P({}) exiting CS".format(tid))
    
    # Scan command line arguments
    thread_count = int(sys.argv[1])
    
    threads = [Thread(target=task, args=(tid,)) for tid in range(thread_count)]
    for t in threads:
        t.daemon = True # die if the program exits
        t.start() # start the thread
    
    # wait for completion
    for t in threads: t.join()
    

    Output

    P(0) sleeping
    P(1) sleeping
    P(2) sleeping
    P(2) entering CS
    P(2) exiting CS
    P(0) entering CS
    P(0) exiting CS
    P(1) entering CS
    P(1) exiting CS
    

    btw, if task() is CPU bound you could do from multiprocessing import Process as Thread to consume multiple cores (using multiple processes instead of threads).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a newbie to Python and have come across the following example in
I am a very newbie in Python I have the following code: from SOAPpy
I have another newbie Python question. I have the following piece of code that
This is a python newbie question: I have the following directory structure: test --
I have written a crude Python program to pull phrases from an index in
I'm a newbie to python and the app engine. I have this code that
I'm newbie in Python. I can't understand why this code does not work: reOptions
Now following my series of python newbie questions and based on another question .
I'm a newbie with a little experience writing in BASIC, Python and, of all
I am a Python newbie. I have this small problem. I want to print

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.