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 8327413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:06:02+00:00 2026-06-09T01:06:02+00:00

I am new to python multithreading and confusing with the code beblow: #!/usr/bin/env python

  • 0

I am new to python multithreading and confusing with the code beblow:

#!/usr/bin/env python

import thread
from time import sleep, ctime

loops = [4, 2]

def loop(nloop, nsec, lock):
    print 'start loop', nloop, 'at:', ctime()
    sleep(nsec)
    print 'loop', nloop, 'done at:', ctime()
    lock.release()

def main():
    print 'starting at:', ctime()
    locks = []
    nloops = range(len(loops))

    for i in nloops:
        lock = thread.allocate_lock()
        lock.acquire()
        locks.append(lock)

    for i in nloops:
        thread.start_new_thread(loop,
                    (i, loops[i], locks[i]))

    for i in nloops:
        while locks[i].locked():
            pass

    print 'all done at:', ctime()

if __name__ == '__main__':
    main()

When I tried to run I got the result below

starting at: Fri Aug 03 17:07:20 2012
start loopstart loop  01  at:at:  Fri Aug 03 17:07:20 2012Fri Aug 03 17:07:20 20
12

loop 1 done at: Fri Aug 03 17:07:22 2012
loop 0 done at: Fri Aug 03 17:07:24 2012
all done at: Fri Aug 03 17:07:24 2012

as you see, the “start” outputs of these two thread were mixed up and I guess there are something conflict, but I don’t understand the details and how to fix it, can anyone help me? thanks.

  • 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-06-09T01:06:03+00:00Added an answer on June 9, 2026 at 1:06 am

    This is pretty usual situation when you work with threads and I/O – threads are writing to stdout simultaneously and their messages become mixed.

    There should be nothing wrong with your multithreading implementation.

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

Sidebar

Related Questions

I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1.
virtualenv --no-site-packages foobar New python executable in foobar/bin/python Installing setuptools............done. What happened here? lsvirtualenv
I am new to Python multithreading coding. I consult the manual for Lock Object
I'm new Python and trying to implement code in a more Pythonic and efficient
New to python and trying to understand multi-threading. Here's an example from python documentation
In my code, I generate new python classes at runtime. For some of them,
I have a super simple python script as defined here import multiprocessing from multiprocessing
Possible Duplicate: How do you read from stdin in python I am new Python
I'm trying to get a handle on multithreading in python. I have working code
So I am trying out the new python code for the google app engine

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.