Skip to content
7 changes: 7 additions & 0 deletions qcodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
from qcodes.version import __version__
from qcodes.process.helpers import set_mp_method
from qcodes.utils.helpers import in_notebook
from qcodes import config

if config.addzmqlogging:
from qcodes.utils.loggingGUI import installZMQlogger
qlogger=installZMQlogger()


#if qcodes.config
# code that should only be imported into the main (notebook) thread
# in particular, importing matplotlib in the side processes takes a long
# time and spins up other processes in order to try and get a front end
Expand Down
7 changes: 7 additions & 0 deletions qcodes/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import os
import tempfile

addzmqlogging = os.environ.get('ZMQLOGGING', 1)

heartbeatfile = os.path.join(tempfile.gettempdir(), r'qcodes-heartbeat.bin' )
49 changes: 49 additions & 0 deletions qcodes/process/heartbeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import mmap

from qcodes import config


def makeHeartBeatFile(bfile, reinit=False):
if (not os.path.exists(bfile)) or reinit:
f = open(bfile, 'wb')
f.write(bytes([99, 1])) # heartbeat on
f.close()


def initHeartBeat(bfile, reinit=False):
''' Initialize connection to heartbeat for writing, use with setHeartBeat '''
if (not os.path.exists(bfile)) or reinit:
f = open(bfile, 'wb')
f.write(bytes([99, 1])) # heartbeat on
f.close()

f = open(bfile, 'a+b')
f.seek(0)
m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE)
m.flush()
# print(m[0])
# print(m[1])
# print('size %d' % m.size() )

return m


def openHeartBeat(bfile):
f = open(bfile, 'r')
m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
return m


def setHeartBeat(m, value):
''' Set heartbeat value '''
m[1] = value


def readHeartBeat(m):
''' Return heartbeat value '''
return m[1]


_bfile = config.heartbeatfile
makeHeartBeatFile(_bfile)
33 changes: 31 additions & 2 deletions qcodes/process/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from uuid import uuid4
import builtins
import logging
import time

QUERY_WRITE = 'WRITE'
QUERY_ASK = 'ASK'
RESPONSE_OK = 'OK'
RESPONSE_ERROR = 'ERROR'

from qcodes import config

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from qcodes.utils import loggingGUI
from qcodes.utils.nested_attrs import NestedAttrAccess
from .qcodes_process import QcodesProcess
from .helpers import kill_queue
Expand Down Expand Up @@ -205,6 +208,11 @@ def close(self):
del self.query_lock


import qcodes.process.heartbeat
from multiprocessing import current_process



class BaseServer(NestedAttrAccess):

"""
Expand Down Expand Up @@ -271,6 +279,9 @@ def __init__(self, query_queue, response_queue, shared_attrs=None):
self._response_queue = response_queue
self._shared_attrs = shared_attrs

self.hb = qcodes.process.heartbeat.openHeartBeat(
qcodes.config.heartbeatfile)

def run_event_loop(self):
"""
The default event loop. When this method returns, the server stops.
Expand All @@ -282,9 +293,27 @@ def run_event_loop(self):
it's not by setting `self.running = False`)
"""
self.running = True

if qcodes.config.addzmqlogging:
self.timeout = 5 # temporary to make heartbeat work
_ = loggingGUI.installZMQlogger()
logging.info('run_event_loop')

ptime=time.time()
while self.running:
query = self._query_queue.get(timeout=self.timeout)
self.process_query(query)
try:
query = self._query_queue.get(timeout=self.timeout)
except mp.queues.Empty:
query = None
if query is not None:
self.process_query(query)
if not qcodes.process.heartbeat.readHeartBeat(self.hb):
logging.info('no heartbeat, stopping process')
self.running = False
else:
if (time.time()-ptime)>.1:
logging.info('heartbeat of %s: alive... %s' % (current_process().name, time.ctime()) )
ptime=time.time()

def process_query(self, query):
"""
Expand Down
1 change: 1 addition & 0 deletions qcodes/tests/instrument_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def __init__(self, name='dummy', gates=['dac1', 'dac2', 'dac3'], **kwargs):
"""
super().__init__(name, **kwargs)

logging.info('create DummyInstrument')
# make gates
for _, g in enumerate(gates):
self.add_parameter(g,
Expand Down
Loading