Search code examples
pythonmultiprocessingpython-multiprocessing

Getting data into a manager.dict and sharing it between processes


I'm currently trying to teach myself how to handle live data by streaming Forza telemetry data from my xbox to my PC. I'm having issues storing that data in a manager.dict and sharing it to other processes. I've created a manager.dict called packets, here is the app.py:

from connectors.forza_connect import forza_connect, forza_record
from telemetry_utils import throttle
from multiprocessing import Manager, Process

if __name__ == '__main__':
    with Manager() as manager:
        packets = manager.dict({})   # Create shared dict for the worker and UI to use
    
        # Start the background data collection process
        data_args = ('motorsport', packets, )                      
        data_process = Process(target=forza_connect, args=data_args)  
        data_process.start()

        throttle_process = Process(target=throttle.show_throttle_text, args=(packets, ))
        throttle_process.start()

        data_process.join()
        throttle_process.join()

This runs forza_connect which collects the data and transforms it into a dictionary, this bit works fine on its own but it doesn't seem to send that data back in to packets. Here's the forza_connect.py:

import socket
from data_utils import forza_data   

def forza_connect(game_version, packets, local_ip='0.0.0.0', port=5555, to_print=False, fd=forza_data):

    ## Connect to Forza
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind((local_ip, port))
    #sock.settimeout(5)

    fd = fd.ForzaData(game_version)

    while True:
        packet, packet_source = sock.recvfrom(1024) # Receive data
        fd.parse(packet)                            # Parse data             
    
        # Append packet to the shared list
        for k, v in fd.to_dict().items():
            packets[k] = v
    
    sock.close()                                    # Close the socket

I want the data collected above to be available to multiple processes, however no data seems to be being stored and no data is getting sent to throttle.py

# Show throttle percentage as text
def show_throttle_text(forza_data):
    while True:
        #print(f"\r throttle percentage: {int(forza_data['throttle'])}", end='', flush=True)
        print(forza_data)

This currently just showing an empty dict. What is it I am mis-understanding about how to use this? Thanks


Solution

  • I solved this by continually exporting data as a json file and reading from that instead of using a shared dictionary.