Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiprocess variable #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions synchrophasor/pmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from select import select
from threading import Thread
from multiprocessing import Queue
from multiprocessing import Process
from multiprocessing import Process, Value
from sys import stdout
from time import sleep
from synchrophasor.frame import *
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(self, pmu_id=7734, data_rate=30, port=4712, ip="127.0.0.1",
self.method = method
self.clients = []
self.client_buffers = []

self.data_rate=Value('i',data_rate)

def set_id(self, pmu_id):

Expand Down Expand Up @@ -98,6 +98,7 @@ def set_configuration(self, config=None):
if not self.cfg1: # If CFG-1 not set use current data stream configuration
self.cfg1 = config
self.cfg1.__class__ = ConfigFrame1
self.set_data_rate(config.get_data_rate())

elif type(config) == ConfigFrame3:
self.cfg3 = ConfigFrame3
Expand Down Expand Up @@ -128,7 +129,7 @@ def set_data_rate(self, data_rate):
self.cfg1.set_data_rate(data_rate)
self.cfg2.set_data_rate(data_rate)
# self.cfg3.set_data_rate(data_rate)
self.data_rate = data_rate
self.data_rate.value = data_rate

# Configuration changed - Notify all PDCs about new configuration
self.send(self.cfg2)
Expand Down Expand Up @@ -159,8 +160,7 @@ def send(self, frame):
buffer.put(frame)


def send_data(self, phasors=[], analog=[], digital=[], freq=0, dfreq=0,
stat=("ok", True, "timestamp", False, False, False, 0, "<10", 0), soc=None, frasec=None):
def send_data(self, phasors=[], analog=[], digital=[], freq=0, dfreq=0,stat=("ok", True, "timestamp", False, False, False, 0, "<10", 0), soc=None, frasec=None):

# PH_UNIT conversion
if phasors and self.cfg2.get_num_pmu() > 1: # Check if multistreaming:
Expand Down Expand Up @@ -220,7 +220,7 @@ def acceptor(self):
self.client_buffers.append(buffer)

process = Process(target=self.pdc_handler, args=(conn, address, buffer, self.cfg2.get_id_code(),
self.cfg2.get_data_rate(), self.cfg1, self.cfg2,
self.data_rate, self.cfg1, self.cfg2,
self.cfg3, self.header, self.buffer_size,
self.set_timestamp, self.logger.level))
process.daemon = True
Expand All @@ -238,8 +238,7 @@ def join(self):


@staticmethod
def pdc_handler(connection, address, buffer, pmu_id, data_rate, cfg1, cfg2, cfg3, header,
buffer_size, set_timestamp, log_level):
def pdc_handler(connection, address, buffer, pmu_id, data_rate, cfg1, cfg2, cfg3, header,buffer_size, set_timestamp, log_level):

# Recreate Logger (handler implemented as static method due to Windows process spawning issues)
logger = logging.getLogger(address[0]+str(address[1]))
Expand All @@ -254,11 +253,6 @@ def pdc_handler(connection, address, buffer, pmu_id, data_rate, cfg1, cfg2, cfg3
# Wait for start command from connected PDC/PMU to start sending
sending_measurements_enabled = False

# Calculate delay between data frames
if data_rate > 0:
delay = 1.0 / data_rate
else:
delay = -data_rate

try:
while True:
Expand Down Expand Up @@ -344,6 +338,11 @@ def pdc_handler(connection, address, buffer, pmu_id, data_rate, cfg1, cfg2, cfg3
if set_timestamp: data.set_time()
data = data.convert2bytes()

# Calculate delay between data frames
if data_rate.value > 0:
delay = 1.0 / data_rate.value
else:
delay = -data_rate.value
sleep(delay)
connection.sendall(data)
logger.debug("[%d] - Message sent at [%f] -> (%s:%d)",
Expand Down