<jupyter_start><jupyter_text>Distribute Workload using ZeroMQIn this section, we will be using ZeroMQ to distribute workload of generating temporal tracesamong distributed workers. Doing so, gives us the ability to get a much higher throughput ofsimulations.<jupyter_code>import sys
import time
import random
from threading import Thread
import struct
import pickle

import zmq
from tqdm import tqdm
import pandas as pd

port = "5556"


context = zmq.Context()
socket = context.socket(zmq.DEALER)
socket.setsockopt(zmq.IDENTITY, b'master')
socket_addr = "tcp://127.0.0.1:%s" % port
socket.bind(socket_addr)

poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)
# Test API function before running worker
from sim_trace import generate_trace_api

data = {
    "arrival_rate": 0.9, 
    "warm_service_rate": 1/2.016, 
    "cold_service_rate": 1/2.163,
    "expiration_threshold": 600, 
    "max_time": 300,
}

generate_trace_api(data)
total_sims = 10000

# generate request that will be sent out to the worker
request = pickle.dumps(data)
def sender(num):
    # send the tasks
    for _ in range(num):
        socket.send(request)

st = Thread(target=sender, args=(total_sims,))
st.start()
pbar = tqdm(total=int(total_sims))

received_sims = 0
results = []
# receive the results
while received_sims < total_sims:
    socks = dict(poller.poll(timeout=5000))
    if socks == {}:
        print("Timeout!")
        break

    if socket in socks and socks[socket] == zmq.POLLIN:
        # print("Message from socket: %s" % struct.unpack("d", socket.recv()))
        results.append(pickle.loads(socket.recv()))
        received_sims += 1
        pbar.update(1)

pbar.close()
res_df = pd.DataFrame(data=results)
print(f"The probability of cold start is: {res_df['prob_cold'].mean():.8f}")

res_df.head()
res_df.mean().to_dict()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11663066954643629, 'fraction_numerical': 0.022138228941684664, 'mean_word_length': 4.324712643678161, 'pattern_counts': {'":': 5, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '149717', 'license': 'MIT', 'n_tokens_mistral': 666, 'n_tokens_neox': 625, 'n_words': 167, 'path': 'examples/07-ZeroMQ-API.ipynb', 'repo_name': 'pacslab/pacssim', 'size': '1852'}