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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:39:39+00:00 2026-06-12T13:39:39+00:00

I want my Python script to be able to run one of its functions

  • 0

I want my Python script to be able to run one of its functions as subprocesses. How should I do that?

Here is a mock-up script of my intention:

#!/urs/bin/env python

def print_mynumber(foo):
    """This function is obviously more complicated in my script.
    It should be run as a subprocess."""
    print(foo)

for foo in [1,2,3]:
    print_mynumber(foo) # Each call of this function should span a new process.
    # subprocess(print_mynumber(foo))

Thank you for your suggestions. It is a little hard for me to formulate the problem correctly, and thus to make the appropriate web search.

  • 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-12T13:39:41+00:00Added an answer on June 12, 2026 at 1:39 pm

    Use the multiprocessing module:

    import multiprocessing as mp
    
    def print_mynumber(foo):
        """This function is obviously more complicated in my script.
        It should be run as a subprocess."""
        print(foo)
    
    if __name__ == '__main__':
        for foo in [1,2,3]:
            proc = mp.Process(target = print_mynumber, args = (foo, ))
            proc.start()
    

    You might not want to be creating one process for each call to print_mynumber, especially if the list foo iterates over is long. A better way in that case would be to use a multiprocessing pool:

    import multiprocessing as mp
    
    def print_mynumber(foo):
        """This function is obviously more complicated in my script.
        It should be run as a subprocess."""
        print(foo)
    
    if __name__ == '__main__':
        pool = mp.Pool()
        pool.map(print_mynumber, [1,2,3])
    

    The pool, be default, will create N worker processes, where N is the number of cpus (or cores) the machine possesses. pool.map behaves much like the Python builtin map command, except that it farms out tasks to the pool of workers.

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

Sidebar

Related Questions

I have a python script that I want always to run in the background.
I have a Iron Python script that I want to run and then have
I want to be able to run cmd.exe from a Python script and a
I want to run a python script from within another. By within I mean
I want to set up a cron job to run a python script, but
I want to start a number of subprocesses in my Python script and then
I want to write a (preferably python) script to modify the content of one
I need to run my Python script as usual, but I want to stop
I'm working on a python script that needs to run between two given times.
newbie python programmer here. I have written a script and want to modify the

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.