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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:52:31+00:00 2026-06-09T20:52:31+00:00

what i want to do is be able to call a function with multiple

  • 0

what i want to do is be able to call a function with multiple threads and get their results.

i have the following code:

(it is an example, the actual code doesn’t simply convert str to int)

from threading import Thread
import time
import Queue

#an example - actual code connects to a server
def str_to_int(arg, queue):
    result = 0
    result = int(arg)
    #sleep to check that they happen at once.
    time.sleep(10)
    queue.put(result)

def combine():
    q1 = Queue.Queue()
    q2 = Queue.Queue()
    q3 = Queue.Queue()

    t1 = Thread(target = str_to_int, args=("111", q1))
    t2 = Thread(target = str_to_int, args=("222", q2))
    t3 = Thread(target = str_to_int, args=("333", q3))

    t1.start()
    t2.start()
    t3.start()

    t1.join()
    t2.join()
    t3.join()

    return (q1.get(),q2.get(),q3.get())

print combine()

this code works. and i get the expected results:

>>> 
(111, 222, 333)

however, there must be a better way to do this.
i plan on having many more threads than 3, but even if i was only staying with 3 – it seems very ugly.

EDIT: i need to be able to know which result came from which thread (ie: from which parameters/arguments that i gave the function)

  • 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-09T20:52:32+00:00Added an answer on June 9, 2026 at 8:52 pm

    Here are some advices:

    1. Queue‘s are thread-safe, so use 1 queue to pass results.
    2. You can create all threads in a cycle and use your queue to pass results. You don’t need to have explicit variable for each thread.

    So here’s what your code might look like:

    def str_to_int(arg, queue):
        result = int(arg)
        queue.put({arg: result})
    
    def combine():
        arguments = ('111', '222', '333')
        q = Queue.Queue()
        threads = []
    
        for argument in arguments:
            t = Thread(target=str_to_int, args=(argument, q))
            t.start()
            threads.append(t)
    
        for t in threads:
            t.join()
    
        return [q.get() for _ in xrange(len(arguments))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary I want to be able to call a JavaScript function from a Flex
I have code in a single R file that I want to be able
I have a winforms app with multiple GUI threads. I want them to be
I want to be able to call a function, that will set one or
I want to be able to call a function from a link (this function
I want to be able to call certain methods and such that are contained
I want my app to be able to tell whether a call was missed,
I want to stop outgoing call and open the application. The app is able
I want to be able to take an image that i have already captured
I want to be able to do the following actions with a form submit

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.