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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:29:10+00:00 2026-05-15T17:29:10+00:00

I am using this code: def startThreads(arrayofkeywords): global i i = 0 while len(arrayofkeywords):

  • 0

I am using this code:

def startThreads(arrayofkeywords):
    global i
    i = 0
    while len(arrayofkeywords):
        try:
            if i<maxThreads:
                keyword = arrayofkeywords.pop(0)
                i = i+1
                thread = doStuffWith(keyword)
                thread.start()
        except KeyboardInterrupt:
            sys.exit()
    thread.join()

for threading in python, I have almost everything done, but I dont know how to manage the results of each thread, on each thread I have an array of strings as result, how can I join all those arrays into one safely? Because, I if I try writing into a global array, two threads could be writing at the same time.

  • 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-15T17:29:11+00:00Added an answer on May 15, 2026 at 5:29 pm

    First, you actually need to save all those thread objects to call join() on them. As written, you’re saving only the last one of them, and then only if there isn’t an exception.

    An easy way to do multithreaded programming is to give each thread all the data it needs to run, and then have it not write to anything outside that working set. If all threads follow that guideline, their writes will not interfere with each other. Then, once a thread has finished, have the main thread only aggregate the results into a global array. This is know as “fork/join parallelism.”

    If you subclass the Thread object, you can give it space to store that return value without interfering with other threads. Then you can do something like this:

    class MyThread(threading.Thread):
        def __init__(self, ...):
            self.result = []
            ...
    
    def main():
        # doStuffWith() returns a MyThread instance
        threads = [ doStuffWith(k).start() for k in arrayofkeywords[:maxThreads] ]
        for t in threads:
            t.join()
            ret = t.result
            # process return value here
    

    Edit:

    After looking around a bit, it seems like the above method isn’t the preferred way to do threads in Python. The above is more of a Java-esque pattern for threads. Instead you could do something like:

    def handler(outList)
        ...
        # Modify existing object (important!)
        outList.append(1)
        ...
    
    def doStuffWith(keyword):
        ...
        result = []
        thread = Thread(target=handler, args=(result,))
        return (thread, result)
    
    def main():
        threads = [ doStuffWith(k) for k in arrayofkeywords[:maxThreads] ]
        for t in threads:
            t[0].start()
        for t in threads:
            t[0].join()
            ret = t[1]
            # process return value here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code taken from here : import smtplib def prompt(prompt): return
This is the snippet of code i'm using now: def countOccurences(a, b) #counts the
While using this code to serialize an object: public object Clone() { var serializer
I am using this code: def req(url, postfields): proxy_support = urllib2.ProxyHandler({http : 127.0.0.1:8118}) opener
I am trying to join 2 strings using this code: def __get_temp(self): return float(self.ask('RS'))
In Python 2.5 I stored data using this code: def GLWriter(file_name, string): import cPickle
I'm using this code to highlight currently active menu tab with Twitter Bootstrap: def
I'm using this code: def copy_part_of_space(row,column,lenght): #Copy String to Presentation Space (15) #Prerequisite Connect
I am trying to decorate an actual class, using this code: def my_decorator(cls): def
Using this code, i am able to send a notification to my own device.

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.