Skip to content

Commit

Permalink
Fixes for running periodic tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
ddetommaso committed Oct 29, 2020
1 parent 3132732 commit 00eab79
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 216 deletions.
10 changes: 4 additions & 6 deletions ex1-producerconsumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,21 @@ def __init__(self, label='Consumer'):
self.connect(self.T3, self.P4, weight=1)
self.connect(self.P4, self.T2, weight=1)

class ProdCons(QNet):
class Net(QNet):

def __init__(self):
QNet.__init__(self, 'ProdCons')
QNet.__init__(self, 'Net')

self.producer = Producer('Producer')
self.buf = Buffer('Buffer')
self.consumer = Consumer('Consumer')
self.prod = Producer('Producer')
self.addNet(self.producer)
self.addNet(self.buf)
self.addNet(self.consumer)
self.connect(self.producer.T1, self.buf.P2, weight=1)
self.connect(self.buf.P2, self.consumer.T2, weight=1)


net = ProdCons()
input(net.state())
net = Net()
for state in net:
print(state)
input(state)
2 changes: 1 addition & 1 deletion ex3-diningphilosophers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ def __init__(self):

net = DiningPhylosophers()

net.start()
net.start_async()
10 changes: 5 additions & 5 deletions ex6-time.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def snap():
qnet = QNet(label='QTimedEx', logging_level=logging.INFO)
t = QTimed('t1sec', 1000)
qnet.addNet(t)
P0 = qnet.createPlace(label='PIN', init_tokens=5)
P1 = qnet.createPlace(label='POUT', target_task=snap)
qnet.connect(P0, t.T_IN, 1)
qnet.connect(t.T_OUT, P1, 1)
qnet.next_until_end()
X = qnet.createPlace(label='X', init_tokens=5)
Y = qnet.createPlace(label='Y', target_task=snap)
qnet.connect(X, t.T_IN, 1)
qnet.connect(t.T_OUT, Y, 1)
qnet.start_async()
60 changes: 60 additions & 0 deletions ex7-periodictask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
BSD 2-Clause License
Copyright (c) 2020, Davide De Tommaso ([email protected])
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

from quantica.core import QNet
from quantica.models import QTimed
import time
import threading
import logging

TASK_PERIOD_MS = 1000.0
TASK_DURATION_MS = 100.0

def onset():
logging.info("Task ONSET")

def offset():
logging.info("Task OFFSET")

qnet = QNet('QTimed', logging_level=logging.INFO)
X0 = qnet.createPlace('X0', init_tokens=5)
X1 = qnet.createPlace('X1', init_tokens=0, target_task=onset)
X2 = qnet.createPlace('X2', init_tokens=0)
X3 = qnet.createPlace('X3', init_tokens=0, target_task=offset)

t_onset = QTimed('onset', PERIOD_MS)
t_offset = QTimed('offset', TASK_DURATION_MS)
qnet.addNet(t_onset)
qnet.addNet(t_offset)
qnet.connect(X0, t_onset.T_IN, 1)
qnet.connect(t_onset.T_OUT, X1, 1)
qnet.connect(t_onset.T_OUT, X2, 1)
qnet.connect(X2, t_offset.T_IN, 1)
qnet.connect(t_offset.T_OUT, X3, 1)

qnet.start_async()
Loading

0 comments on commit 00eab79

Please sign in to comment.