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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:45:28+00:00 2026-06-15T02:45:28+00:00

I want to share a variable between two threads using atomic operations in the

  • 0

I want to share a variable between two threads using atomic operations in the interpreter, as described here http://effbot.org/zone/thread-synchronization.htm. A simple assignment (single bytecode operation) of a core data type should be thread safe, beacuse of the GIL in python < 3.2. So far the theory. The follwing code can be run in either master or slave mode (-m or -s). The master mode does constantly send data via UDP. The slave mode does create a thread to read data from a udp port and update a variable on each received packet.

The example code does pass the shared variable as an argument to the thread on creation. I’ve tried also by using a global variable or passing a thread local store to the thread.

The result is alwas the same. Inside the read_time_master thread the variable gets assigned. But in the main thread, the value of shared variable isn’t updated.

#!/usr/bin/env python

import socket
import itertools
import multiprocessing
from optparse import OptionParser
from time import sleep

PORT = 1666

def read_time_master(sock, time_master):
   while True:
     time_master = float(sock.recvfrom(1024)[0])

def main():
    time_master = 0.0
    p = OptionParser()
    p.add_option('--master', '-m', action='store_true')
    p.add_option('--slave', '-s', action='store_true')
    options, arguments = p.parse_args()
    if options.master or options.slave:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
    if options.master:
        sock.connect(('127.0.0.1', PORT))
    if options.slave:
        sock.bind(('0.0.0.0', PORT))
        recv_thread = multiprocessing.Process(target=read_time_master, args=(sock, time_master))
        recv_thread.start()

    for time in itertools.count():
        print time
        if options.slave:
            print "master: %f" % time_master # -> not updated from other thread
        if options.master:
            try:
                sock.send(str(time))
            except socket.error:
                pass
        sleep(1)

    if options.master or options.slave:
        sock.close()

if __name__ == '__main__':
    main()
  • 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-15T02:45:29+00:00Added an answer on June 15, 2026 at 2:45 am

    You’re using multiprocessing, not threading, which isn’t helping your situation. If you were using threading.Thread to create the background worker you’d probably be able to get what you needed by simply throwing in a global time_master call within the function that’s being controlled by your background operation. Because you’re using multiprocessing, not threading, you will likely need to look into the multiprocessing.Queue class for containers that you can use to pass information back and forth between your processes or to synchronize them. You can also create variables that are shared between the processes as well (all of this is covered in the multiprocessing documentation / examples at the Python Homepage

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

Sidebar

Related Questions

I want to share a variable between multiple threads like this: boolean flag =
I want to share some image Icons between two applications. I stored the icons
I currently have code to share a variable between two entry points in my
I'm trying to share a structure between two threads that is not a global
I want to share a variable between more than one independent C executables in
hi i want to share a variable between 2 form. 2 forms is in
I want to share my session variables in PHP using many subdomains. I have
I want to share a dictionary between my processes as follows: def f(y,x): y[x]=[x*x]
I want to share(and give control of) my Ubuntu 12.04 desktop to my brother(using
I want to share my experience of using maven through a proxy. You would

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.