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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:22:30+00:00 2026-06-15T10:22:30+00:00

I am trying to build a program in python that relies on multiple threads,

  • 0

I am trying to build a program in python that relies on multiple threads, with data shared between the threads. I am trying to avoid doing this with the global keyword, but not getting anywhere so far.

As a simple example (code below), my main() function spawns one thread, thread1, which should be able to access the variable count, in this case just to print it. At the same time, main() iterates this variable, and thread1 should be able to see count changing. Some self contained code here:

import threading
import time

class myThread (threading.Thread):

    def __init__(self, threadID):
        self.threadID = threadID
        threading.Thread.__init__(self)

    def run(self):
        global count
        for i in range(10):
            print "count is now: ", count, " for thread ", self.threadID, "\n"
            time.sleep(5)


def main():

    global count
    count = 0

    # spawn one or more threads
    thread1 = myThread(1)
    thread1.start()

    for i in range(20):
        time.sleep(2)
        count = count + 1

    # wait for thread1 to finish
    thread1.join()

main()

When reading about threads in python, I haven’t found any other ways to do this than using global. However, when reading about global, most people say you should very rarely use it, for good reasons, and some people even think it should be removed from python altogether. So I am wondering if there is actually an alternative way of getting thread1 to “passively” detect that main() has iterated count, and access that new value? E.g. I don’t know much about python and pointers (do they even exist in python?), but I would in any case assume this is exactly what global achieves.

Ideally I would be able to call a thread1 method from main() to set a new self.count whenever count is iterated, but as thread1 has a run() method that is blocking I can’t see how to do this without having another independent thread inside thread1, which seems too complicated.

  • 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-15T10:22:31+00:00Added an answer on June 15, 2026 at 10:22 am

    You can create the thread object and fill the class attributes.

    import threading
    class MyThreadClass(threading.Thread):
      def __init__(self, fruits):
        threading.Thread.__init__(self)
        self.fruits = fruits    
    
      def run(self):
        self.fruits.append('banana')  
    
    list_fruit = ['apple', 'orange']    
    print 'BEFORE:', list_fruit
    thread = MyThreadClass(list_fruit)
    thread.start() 
    thread.join() #Wait for the thread to finish
    print 'AFTER:', list_fruit
    

    Output:

    BEFORE: ['apple', 'orange']
    AFTER: ['apple', 'orange', 'banana']
    

    For your case, you can try:

    import threading
    import time
    
    class myThread (threading.Thread):
    
        def __init__(self, threadID):
            self.threadID = threadID
            self.count = 0
            threading.Thread.__init__(self)
    
        def run(self):
            for i in range(10):
                print "count is now: ", self.count, " for thread ", self.threadID, "\n"
                time.sleep(5)
    
    
    def main():    
        # spawn one or more threads
        thread1 = myThread(1)
        thread1.start()
    
        for i in range(20):
            time.sleep(2)
            thread1.count = thread1.count + 1
    
        # wait for thread1 to finish
        thread1.join()
        print thread1.count
    
    main()
    

    If you want to use the same count shared between multiple thread, you can put your count in an list containing just one element.
    This way, when assigning count to thread attribute, it will not be a hard copy.

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

Sidebar

Related Questions

I'm trying to build a program that goes like this: User enters 3 numbers.
I am trying to build a program that accepts an array of integers as
I'm trying to build a small program that hosts vst effects and I would
When trying to build a simple test program that uses atomic operations, I get
I want to build a python program that get as input a path to
I have a python program I wrote that I am trying to compile with
I am trying to build some program that detects if a server of some
I'm trying to build a Python program with the structure shown at the end.
I am trying to build an executable for my python program like so: from
I'm trying to build a regular expression that matches regular expressions between two forward

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.