Giter Club home page Giter Club logo

Comments (1)

TKaluza avatar TKaluza commented on June 1, 2024

In Windows 11 with the new 26 pyzmq version ipc seems to work. So this can be closed propably?

import zmq
import os
import numpy as np
import logging
from multiprocessing import Process

# Configure logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Create a random numpy array of 1MB
array_size = 1 * 1024 * 1024 // 8  # 1MB of float64
random_array = np.random.rand(array_size)

# Save the array to a file
filename = 'random_array.npy'
np.save(filename, random_array)
logging.debug(f'Saved random array to {filename}')

# Define the IPC endpoint
ipc_endpoint = 'ipc://test_ipc.ipc'


def server():
    logging.debug('Server: Starting...')
    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.bind(ipc_endpoint)
    logging.debug(f'Server: Bound to {ipc_endpoint}')

    message = socket.recv()
    logging.debug('Server: Received message')

    # Save the received message to a numpy file
    received_array = np.frombuffer(message, dtype=np.float64)
    np.save('received_array.npy', received_array)
    logging.debug('Server: Saved received array to received_array.npy')

    # Load the original array from file
    original_array = np.load(filename)
    logging.debug('Server: Loaded original array from file')

    # Load the received array from file
    loaded_received_array = np.load('received_array.npy')
    logging.debug('Server: Loaded received array from file')

    # Compare the arrays
    if np.array_equal(loaded_received_array, original_array):
        socket.send_string("SUCCESS")
        logging.info("SUCCESS for IPC")
        logging.debug('Server: Arrays match. Sent SUCCESS to client')
    else:
        socket.send_string("FAILURE")
        logging.debug('Server: Arrays do not match. Sent FAILURE to client')


def client():
    logging.debug('Client: Starting...')
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.connect(ipc_endpoint)
    logging.debug(f'Client: Connected to {ipc_endpoint}')

    # Load the original array from file
    original_array = np.load(filename)
    logging.debug('Client: Loaded original array from file')

    # Send the array as bytes
    socket.send(original_array.tobytes())
    logging.debug('Client: Sent data to server')

    result = socket.recv_string()
    logging.debug(f'Client: Received result from server: {result}')
    return result


if __name__ == '__main__':
    logging.debug('Main: Starting server and client processes')

    server_process = Process(target=server)
    server_process.start()
    logging.debug('Main: Server process started')

    client_process = Process(target=client)
    client_process.start()
    logging.debug('Main: Client process started')

    client_process.join()
    server_process.join()
    logging.debug('Main: Server and client processes joined')

    # Clean up the generated files
    os.remove(filename)
    os.remove('received_array.npy')
    logging.debug('Main: Cleaned up generated files')

from libzmq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.