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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:58:28+00:00 2026-06-12T03:58:28+00:00

I am having trouble with a sleep statement hanging my multithreading function. I want

  • 0

I am having trouble with a sleep statement hanging my multithreading function. I want my function to go about it’s buisness while the rest of the program runs. Here is a toy that recreates my problem:

import multiprocessing, sys, time

def f(icount, _sleepTime = 1):
    for i in range(icount):
        time.sleep(_sleepTime)
        print(_sleepTime)

def main(args):
    m = multiprocessing.Process(target = f, args=(4, ))
    m.run()
    # f should be sleeping for 1 second so this print statement should come first
    print(m.is_alive())


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

can anyone explain why this code outputs:

1
1
1
1
False

instead of:

True
1
1
1
1

#

EDIT

#

I eventually want to run this function on a schedual, and test if it is running before I execute the function. This is an example:

import multiprocessing, sys, time

def f(icount, _sleepTime = 1):
    for i in range(icount):
        time.sleep(_sleepTime)
        print(_sleepTime)

def main(args):
    m = multiprocessing.Process(target = f, args=(4, ))
    for i in range(15):
        time.sleep(.5)
        if not m.is_alive():
            # m.start throws an error after first run
            m.run()
        print("{}".format(m.is_alive()))


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
  • 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-12T03:58:29+00:00Added an answer on June 12, 2026 at 3:58 am

    Use start and join instead of run:

    import multiprocessing, sys, time
    
    def f(icount, _sleepTime = 1):
        for i in range(icount):
            time.sleep(_sleepTime)
            print(_sleepTime)
    
    def main(args):
        m = multiprocessing.Process(target = f, args=(4, ))
        m.start()
        # f should be sleeping for 1 second so this print statement should come first
        print(m.is_alive())
        m.join()
    
    
    if __name__ == "__main__":
        sys.exit(main(sys.argv[1:]))
    

    #

    EDIT

    #

    Again, use start and join instead of run:

    import multiprocessing, sys, time
    
    def f(icount, _sleepTime = 1):
        for i in range(icount):
            time.sleep(_sleepTime)
            print(_sleepTime)
    
    def create_process():
        return multiprocessing.Process(target = f, args=(4, ))
    
    def main(args):
        m = create_process()
        m.start()
        for i in range(15):
            time.sleep(.5)
            if not m.is_alive():
                # m.start throws an error after first run
                print("restarting")
                m.join()
                m = create_process()
                m.start()
            print("{}".format(m.is_alive()))
        m.join()
    
    
    if __name__ == "__main__":
        sys.exit(main(sys.argv[1:]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble stopping a while-loop in the KeyListener function. Every 10 seconds the
Having trouble with each function... Will try to explain by example... In my code,
I am having trouble with Quartz Scheduler. To be practical here you find my
Hey, I'm having some trouble here... How can I delete an entire text from
I am learning python and am having trouble getting this program to work correctly.
I'm having some trouble understanding this. Here's what's happening. I'm spawning a new thread
Having trouble finding a solution for my situation here. Sorry if this has been
Having trouble here and not quite sure how to do it. I've attempted jQuery
I'm having trouble trying to figure out how to use a function contained in
I am having trouble using the MapMaker from google-guava. Here is the code: package

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.