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

The Archive Base Latest Questions

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

I need to know how to write a program that runs two threads: 1.

  • 0

I need to know how to write a program that runs two threads:
1. a thread the updates the position(values) of an object
2. a thread that runs two methods in it based on the values from the first thread
All of this in a loop

This is the current code that I have and I want to changed as I described, not that the value are form the valueList and the activate(valueList) is a method that contains several smaller methods in it. If you don’t mind i would love to have an example to solving this.Thanks

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('59.191.193.42',5555))


screenw = 0
screenh = 0
while 1:
    client_socket.send("loc\n")
    data = client_socket.recv(8192)
    valueList = data.split()

    if (not(valueList[-1] == "eom" and valueList[0] == "start")):
        #print "continuing.."
            continue

        if(screenw != int(valueList[2])):
            screenw = int(valueList[2])
            screenh = int(valueList[3])

    activate(valueList)
  • 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-12T00:58:44+00:00Added an answer on June 12, 2026 at 12:58 am

    As others have pointed out, It doesn’t look like threading is actually going to solve your problem. Especially because Python code is subject to the Global Interpreter Lock, which basically means theading will only help you if your code is IO-bound (reading large files from disk, waiting on a slow network connection, etc). If your program is CPU-bound and you really want to take advantage of parallel processing, multiprocessing is the way to go. With multiprocessing, you trade some memory overhead and a little latency (when the process is created and during inter-process communication) for the ability to take advantage of multi-core CPUs.

    Just on the off chance that parallel processing does turn out to be useful for your program, or you’re just curious, I present the following code sample. Disclaimer, I haven’t so much as tried to import this module, so consider it pseudocode.

    import socket
    from multiprocessing import Process, Queue, Value
    from ctypes import c_bool
    
    HOST = '198.51.100.0'
    PORT = 8080
    
    # This function will be run in a child process
    def update_proc(data_queue, update_queue, quit_flag):
        while not quit_flag.value:
            data = data_queue.get()
            # do something with the data...
            update_queue.put(data)
        print "Closing child update process"
    
    # This function will be run in a child process
    def activate_proc(update_queue, quit_flag):
        while not quit_flag.value:
            data = update_queue.get()
            # do something with the data...
        print "Closing child activate process"
    
    # main process begins execution here, if module is run from the terminal
    if __name__ == "__main__":
        # Connect to remote host over TCP
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect((HOST,PORT))
    
        # Set up a Queue to pass data to the update process, and another one
        # for the two children to communicate
        data_queue = Queue()
        update_queue = Queue()
    
        # The quit_flag Value is a *very* primitive way to signal the child 
        # processes to quit. I'm sure there are better ways to do this, but I'm 
        # tired and can't think of any right now.
        quit_flag = Value(c_bool, False)
    
        # Create two child processes, pass a reference to the Queue to each
        update = Process(target=update_proc, args=(data_queue, update_queue, quit_flag))
        activate = Process(target=activate_proc, args=(update_queue, quit_flag))
    
        update.start()
        activate.start()
    
        # Read data from the TCP socket, push it onto the data_queue
        while True:
            client.sendall("loc\n")
            data = client.recv(8192)
            if not data:
                print "network connection closed by client"
                break
            data_queue.put(data)
    
        # Join with child processes before closing
        print "All done, closing child processes"
        update.join()
        activate.join()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a program that will take a existing local windows user,
I need to write a program that, when minimized, lives in the System Tray,
I need to write a perl program that accept command lines arguments which can
I need to write a program that scrambles a 4 letter string inputted by
I need to write a simple program that ask the user to insert 4
I need to write a program in C# that facilitates the input of logical
i need to write a program that auto detects windows and unix machines in
Cappuccino is one. With Cappuccino, you don't need to know HTML. You'll never write
I'd like to know do I need to write destructor in classes when I
Need to know this so that i could send DTMF and that is going

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.