Example #1
0
def main():

    if len(sys.argv) < 2:
        print "Insufficient arguments provideed \n Code Usage- python ProduceRecords.py <config File>"
        exit()
    else:
        ConfigFile = open(sys.argv[1], "r")
        Configs = ConfigFile.readlines()
        inputTopicName = "Default"
        brokerList = ["localhost:9092"]
        timeInterVal = 30
        for config in Configs:
            if "inputTopicName" in config:
                inputTopicName = config.split("=")[1].strip("\n")

            if "brokerList" in config:
                brokerList = config.split("=")[1].strip("\n").split(",")

            if "timeInterVal" in config:
                timeInterVal = int(config.split("=")[1].strip("\n"))

    print "Config Sucessfully Captured\n Details:\n1.TopicName=%s\n2.BrokerList=%s\n3.TimeInterval=%s" % (
        inputTopicName, str(brokerList), timeInterVal)

    Producer = ProduceRecords(brokerList, inputTopicName, timeInterVal)
    Producer.messgaeCreator()
    def __init__(self, *args, **kwargs):
        Producer.__init__(self, *args, **kwargs)
        if 'fname' in kwargs:
            fname = kwargs['fname']

        self.log.info('Opening frame source %s', fname)
        self.resource = cv2.VideoCapture(fname)
Example #3
0
    def Equilibrium_Ind(self):
        # Obtain the output list with the correct list length
        # Format of output list: [C1G1 C1G2 C2G1 C2G2 C1F1 C1F2 C2F1 C2F2 P1F1 P1F2 P2F1 P2F2]
        output_list_length = self.c * (self.g + self.f) + self.g * self.f
        output_list = []
        # Generate initial output list values for the optimization
        for i in range(output_list_length):
            output_list.append(random.uniform(0.3, 5))

        # Solve the optimization problem
        # G: An instance of the social planner, who determines the welfare function and constraints
        G = SP(self.c, self.g, self.f, self.A, self.B, self.T, self.e1)
        # res: Results of optimization problem
        res = minimize(G.Welfare,
                       output_list,
                       method='SLSQP',
                       constraints=G.Constraints_Ind(output_list))
        for i in range(100):
            output_list = []
            for i in range(output_list_length):
                output_list.append(random.uniform(0.3, 5))
            compare = minimize(G.Welfare,
                               output_list,
                               method='SLSQP',
                               constraints=G.Constraints_Ind(output_list))
            if res.fun > compare.fun:
                res = compare

        P = Producer(self.c, self.g, self.f, self.e1)
        real = P.Production(res.x)
        for i in range(self.g * self.c):
            if i == 0 or (i % self.g == 0 and i < self.g * self.c):
                res.x[i:i + self.g] = real / self.c
        welfare = G.Welfare(res.x)
        return welfare
def main():
    # handle user input
    from_date_string = str(
        raw_input("Please specify a Start Date with format ddmmYYYY: "))
    # raw input in python2 is the same as input in python3 (returns a string instead of a python expression)
    to_date_string = str(
        raw_input("Please specify a End Date with format ddmmYYYY: "))
    locale.setlocale(locale.LC_ALL, '')
    from_date = datetime.datetime.strptime(from_date_string,
                                           '%d%m%Y').isoformat()
    to_date = datetime.datetime.strptime(to_date_string, '%d%m%Y')
    tmp_date = to_date + datetime.timedelta(1)
    to_date = tmp_date.isoformat()

    threads = list()
    stopper = threading.Event()

    consumer = Consumer()
    producer = Producer(from_date=from_date, to_date=to_date)

    threads.append(consumer)
    threads.append(producer)

    handler = SignalHandler(stopper, threads)
    signal.signal(signal.SIGINT, handler)

    producer.start()
    consumer.start()
Example #5
0
 def __start_qualification(self):
     self.buffer = __buffer_sensor__()
     self.producer_qualification = Producer(self.buffer, self.sensor,
                                            self.race, 'qualification')
     self.consumer_qualification = Consumer(self.buffer, self.race,
                                            self.publisher, 'qualification')
     self.producer_qualification.start()
     self.consumer_qualification.start()
     return ''
Example #6
0
 def __start_race(self):
     self.buffer = __buffer_sensor__()
     self.producer_race = Producer(self.buffer, self.sensor, self.race,
                                   'race')
     self.consumer_race = Consumer(self.buffer, self.race, self.publisher,
                                   'race')
     self.producer_race.start()
     self.consumer_race.start()
     return ''
 def __init__(self, name=None, symbols=None, **kwargs):
     Producer.__init__(self, name=name, **kwargs)
     self.sina = V('Sina')
     if symbols is None:
         self.symbols = self.sina.get_symbols()
     else:
         self.symbols = symbols
     [self.szSymbols, self.shSymbols] = util.split_symbols(self.symbols)
     self.szTime = datetime(1970, 1, 1)
     self.shTime = datetime(1970, 1, 1)
 def run(self):
     # Setting producer.
     producer = Producer(self.semaphore)
     producer.setPort(9092)
     producer.setIP("10.0.0.212")
     producer.startConnection()
     #sending messages.
     for x in range(0, 100):
         print("Send message....")
         self.semaphore.acquire()
         producer.sendMessage(Point(5 * x, 10 * math.sin(5 * x + 10) + 40))
         self.semaphore.release()
    def run(self):
        roomName = input('Please enter the name of the room: \n')
        clientName = input('Please enter the name of the client: \n')
        privateKey = bytearray(123)
        res = self.enterRoom(roomName, clientName, privateKey)

        if (res):
            print('Entered')
            self.consumer = Consumer(clientName, self.channel, self.queue_name)
            self.consumer.start()
            self.producer = Producer(clientName, self.channel, roomName,
                                     privateKey)
            self.producer.start()
Example #10
0
 def Constraints(self,output_list):
     # Generate an instance of Producer
     P = Producer(self.c,self.g,self.f,self.e1)
     # Generate an instance of Total
     T = Total(self.c,self.g,self.f)
     # Construct the constraints function
     # First constraint: Total Production = Total Consumption
     # Second constraint: Total factor supplied = Total factor demanded
     # Third constraint: All result values are non-negative
     Cons = ({'type': 'eq','fun' : lambda x:(T.total_consumption(x)-P.Production(x))},
     {'type': 'eq','fun' : lambda x:(T.total_factor_dd(x)-T.total_factor_ss(x))},
     {'type': 'ineq','fun': lambda x:x},)
     return Cons
def generate_predictions():
    """
    Spawns a producer which reads video frames and result collector to yield results.
    Iterates through result collector and yields results to client as they are available
    Uses event-stream to keep connection open.

    :return: Flask response (event-stream)
    """
    video_name = request.args.get('video_name')

    producer = Producer(sender_port=args.producer_send_port,
                        video_path=f'data/{video_name}')
    Process(target=producer.run).start()

    result_collector = ResultCollector(receiver_port=args.consumer_send_port)

    def generate():
        try:
            for frame_idx, img, peak_points in result_collector.run():
                img_data_url = encode_img_to_data_url(img=img)
                peak_points = peak_points.numpy().tolist()
                res = json.dumps({
                    'img': img_data_url,
                    'peak_points': peak_points,
                    'frame_idx': frame_idx
                })
                yield f'data: {res}\n\n'
            yield 'event: done\ndata: {}\n\n'
        except GeneratorExit:
            log.debug('APP: Client closed connection')

    return Response(generate(), mimetype='text/event-stream')
Example #12
0
    def __init__(self, securities):
        self.consumer_group = []
        for security in securities:
            self.consumer_group.append(Consumer(security, self))

        self.producer = Producer(self.consumer_group, self)
        self.flag = AtomicNumber()
        self.i = 0
Example #13
0
    def createProducer(self, connect, stats, routingKey):
        channel = connect.channel()
        if self.producerTxSize > 0:
            channel.tx_select()
        if self.confirm >= 0:
            channel.confirm_delivery()
        channel.exchange_declare(self.exchangeName, self.exchangeType)

        return Producer(channel, self.exchangeName, self.exchangeType,
                        routingKey, self.randomRoutingKey, self.flags,
                        self.producerTxSize, self.producerRateLimit,
                        self.producerMsgCount, self.timeLimit, self.minMsgSize,
                        stats)
Example #14
0
class TwitterStreamListener(StreamListener):
    def __init__(self):
        print "TwitterStreamListener started"
        self.counter = 0
        self.last_sent = time.time()
        self.producer = Producer(host, exchange, routing_type, routing_key)
        #self.pos_sent_polarity = 0.0
        #self.neg_sent_polarity = 0.0

    def broadcast(self, message_text): #, pos, neg):
        if self.producer is None:
            print "Cannot send because self.producer is None"
            return False

        m = Message("twitter-volume", message_text)
        #m.add_field("pos_sent", str(pos))
        #m.add_field("neg_sent", str(neg))

        if self.producer.send(m.str()):
            print "[%d] Transmission successful" % m.get_time()
        else:
            print "[%d] Transmission failed" % m.get_time()

    def on_data(self, data):
        structured = json.loads(data)
        if 'text' not in structured:
            return True

        print "Received tweet with keyword (counter=%d)" % self.counter

        #blob = TextBlob(structured['text'])
        #dur_sent = blob.sentiment.polarity
        #if dur_sent < 0.0:
        #    self.neg_sent_polarity += dur_sent
        #else:
        #    self.pos_sent_polarity += dur_sent

        #if print_each:
        #    print "[%d] %s polarity = %s" % (self.counter, structured['text'], str(dur_sent))
          
        self.counter += 1
        if time.time() - self.last_sent > diff_time:
            self.last_sent = time.time()
            self.broadcast(str(self.counter)) # self.pos_sent_polarity, self.neg_sent_polarity)
            #self.pos_sent_polarity = 0.0
            #self.neg_sent_polarity = 0.0
            self.counter = 0
        return True

    def on_error(self, status):
        print status
class Client():
    def __init__(self, address):
        self.connection = pika.BlockingConnection(
            pika.ConnectionParameters(address))
        self.channel = self.connection.channel()

    def run(self):
        roomName = input('Please enter the name of the room: \n')
        clientName = input('Please enter the name of the client: \n')
        privateKey = bytearray(123)
        res = self.enterRoom(roomName, clientName, privateKey)

        if (res):
            print('Entered')
            self.consumer = Consumer(clientName, self.channel, self.queue_name)
            self.consumer.start()
            self.producer = Producer(clientName, self.channel, roomName,
                                     privateKey)
            self.producer.start()

    def enterRoom(self, room_name, name, privateKey):
        # Hash the room name and client name with private key
        room_name = hmac.new(privateKey, room_name.encode(),
                             hashlib.sha256).hexdigest()
        name = hmac.new(privateKey, name.encode(), hashlib.sha256).hexdigest()

        # Generate a random unique key for the queue name
        unique_key = ''.join(
            random.SystemRandom().choice(string.ascii_uppercase +
                                         string.digits) for _ in range(64))

        # set queue names
        self.queue_name = room_name + ":" + name
        self.exchange = self.channel.exchange_declare(exchange=room_name,
                                                      exchange_type='fanout')
        self.queue = self.channel.queue_declare(queue=self.queue_name)
        self.channel.queue_bind(exchange=room_name, queue=self.queue_name)
        return True
Example #16
0
class ProduceRecords():
    def __init__(self, brokerList, topicName, interval=30):

        self.keys = ["key1", "key2", "key3", "key4", "key5"]
        self.topicName = topicName
        self.Producer = Producer(brokerList)
        self.interval = interval

    def messgaeCreator(self):

        Status = True
        while Status:
            for key in self.keys:
                try:
                    self.Producer.sendData(key, self.createValue(key),
                                           self.topicName)
                    time.sleep(random.randint(1, 4))
                except Exception, e:
                    print "Exception Encountered"
                    print e
                    exit()

            time.sleep(int(self.interval))
Example #17
0
 def __init__(self,
              name=None,
              username=None,
              pwd=None,
              symbols=None,
              hq='hq_pjb',
              query=['quotation', 'orders', 'deal', 'info'],
              **kwargs):
     Producer.__init__(self, name=name, **kwargs)
     self.ip = util.get_client_ip()
     self.hq = hq
     self.query = query
     # 登录模块在V('Sina')中
     self.sina = V('Sina')
     self.is_login = self.login()
     if not self.is_login:
         self.logger.error(u'登录失败,请核对账号和密码')
         sys.exit(-1)
     if symbols is None:
         self.symbols = self.sina.get_symbols()
     else:
         self.symbols = symbols
     self.websockets = dict()
Example #18
0
def main(argv):
    # Default values
    assignment_url = 'https://raw.githubusercontent.com/rpfk/python-kafka-producer-assignment/master/assignment.json'
    kafka_address = '192.168.21.21:9092'

    # Check if the required command line arguments are given
    try:
        opts, args = getopt.getopt(argv, "",
                                   ["kafka-address=", "assignment-url="])
    except getopt.GetoptError:
        print 'run.py --kafka-address=<kafka-address> --assignment-url=<assignment-url>'
        sys.exit()

    # Get kafka_address from the command line arguments
    for opt, arg in opts:
        if opt == "--kafka-address":
            kafka_address = arg
        elif opt == "--assignment-url":
            assignment_url = arg

    # Check if kafka address is set correctly
    try:
        kafka_address
    except NameError:
        print 'kafka address is not set correctly, check the kafka address'
        print 'run.py --kafka-address=<kafka-address> --assignment-url=<assignment-url>'
        sys.exit()

    # Check if assignment is set correctly
    try:
        assignment_url
    except NameError:
        print 'assignment url is not set correctly, check the assignment url'
        print 'run.py --kafka-address=<kafka-address> --assignment-url=<assignment-url>'
        sys.exit()

    # get the assignment from the assignment url
    assignment_buffer = StringIO()
    c = pycurl.Curl()
    c.setopt(c.URL, assignment_url)
    c.setopt(c.WRITEDATA, assignment_buffer)
    c.perform()
    c.close()
    assignment = assignment_buffer.getvalue()

    # Run the producer if everything is set correctly
    Producer(kafka_address, assignment)
Example #19
0
def init():

    global curTime, factoryEvents, simulator_state, credit, profit, idleProducers
    curTime = 0
    factoryEvents = np.empty(shape=[0], dtype=FactoryEvent)
    simulator_state = True
    credit = 100
    profit = 0
    idleProducers = np.empty(shape=[0], dtype=int)

    for i in range(0, NUM_OF_PRODUCER):
        producers[i] = Producer()
        idleProducers = np.append(idleProducers, producers[i].getIndex())
        print(producers[i])
    for i in range(0, NUM_OF_PRODUCT):
        products[i] = Product(PRODUCE_TIME[i])
        PRODUCT_NAME[i] = i
        print(products[i])
    loadConsumers()
    def run(self):
        producer = Producer()
        # producer.setPort(9092)
        # producer.setIP("52.186.81.99")

        producer.startConnection()
        self.points.producerTime = time.time()
        self.semaphore.acquire()
        for x in range(0, 100):
            print("PRODUCER: Sending message....")
            producer.sendMessage(Point(5 * x, 10 * math.sin(5 * x + 10) + 40))
            if (x % 10 == 0):
                self.semaphore.release()
                time.sleep(self.delay)
                self.semaphore.acquire()
Example #21
0
 def addFactory(self, last_x = 0, last_y = 0):
     randx = random.random()-.5
     signx = -1 if randx<0 else 1
     randy = random.random()-.5
     signy = -1 if randy<0 else 1
     if 1:#random.random()>.5:
         x = round(randx*WINDOW_WIDTH/self.scale)
         y = round(randy*WINDOW_HEIGHT/self.scale)
     else:
         x = round(signx*((abs(randx)**2)*(WINDOW_WIDTH/self.scale*4)) + last_x)
         y = round(signy*(2+(abs(randy)**2)*(WINDOW_HEIGHT/self.scale*4)) + last_y)
     if (x, y) in self.filledSpaces:
         return self.addFactory(last_x, last_y)
     else:
         bmax = len(self.button_dict)+1
         b = math.floor(random.random()*4)+1
         if b > bmax: b = bmax
         producer = Producer(self.getType(), self, assemblerimg[int(b)-1], x, y, button=BUTTON_DICT_M[b])
         self.factories.add(producer)
         self.filledSpaces.append((x,y))
         return producer
Example #22
0
def main():
	stack = Manager().list()
	n = Semaphore(0)
	s = Semaphore(1)
	
	# Create Producer and Consumer threads
	producer1 = Producer(stack, n, s, 1)
	#producer2 = Producer(stack, n, s, 2)
	#producer3 = Producer(stack, n, s, 3)
	#producer4 = Producer(stack, n, s, 4)
	#producer5 = Producer(stack, n, s, 5)
	consumer = Consumer(stack, n, s)
	
	# Start all the threads
	producer1.start()
	#producer2.start()
	#producer3.start()
	#producer4.start()
	#producer5.start()
	consumer.start()
	
	time.sleep(2)
	
	consumer.stop()
	time.sleep(0.1)
	producer1.stop()
	
	# Wait for threads to finish
	producer1.join()
	#producer2.join()
	#producer3.join()
	#producer4.join()
	#producer5.join()
	consumer.join()
	
	print("Done.")
Example #23
0
    def MainLoop(self):
        self.button_dict = {}
        self.scale = 40000/self.x_view
        self.filledSpaces = [] # add coordinate tuples whenever a space if filled e.g. (x, y)
        self.setRhythms(self.level) # 4 rhythms, each expressed as a array of 8 ones or zeros (for each 8th note beat)
        self.quota = self.score+100*sum(map(lambda r: sum(r), self.rhythms))-200

        # sprite groups so we can render everything all at once
        self.allConveyorSprites = pygame.sprite.Group()
        self.factories = pygame.sprite.Group()
        self.allSprites = pygame.sprite.Group(self.allConveyorSprites,self.factories)

        #Background music from the following music
        #http://audionautix.com/?utm_campaign=elearningindustry.com&utm_source=%2Fultimate-list-free-music-elearning-online-education&utm_medium=link
        myMusic.load('BigCarTheft.ogg')
        myMusic.play(-1)
        self.songTime = 0  # how far along in the song the system thinks we are in ms
        self.lastReportedPlayheadPosition = 0  # the last reported song position from the mixer
        self.beatStep = 0
        self.beatProgress = 0  # number of beats that have passed
        self.beatPos = 0  # position inside our current beat
        self.renderTeddy = False
        self.counter= 0
        self.frameCounter = 0
        self.fps = 0
        self.timePassed = 0
        self.displayDebug = False
        self.beatAlternate = True
        self.gamestart = False

        while not self.gamestart:
            self.screen.fill((20, 20, 20))  # setting background color
            font ="norasi"
            font_size1 = 100
            font_size2 = 30
            k = 250
            if self.health <= 0:
                msg1 = "Game Over"
                msg2 = "PRESS ENTER TO TRY AGAIN"
                msg3 = "Final Score: " + str(self.score)
                msg4 = "Final Level: " + str(self.level)
                msg_location1 = (120+k,100)
                msg_location2 = (165+k,300)
                msg_location3 = (270+k,370)
                msg_location4 = (270+k,420)
                self.MsgRender(self.screen, font, font_size2, msg3, msg_location3,(255,255,255))
                self.MsgRender(self.screen, font, font_size2, msg4, msg_location4,(255,255,255))
            elif self.level == 1:
                msg1 = "Manual Control"
                msg2 = "PRESS ENTER TO ENJOY!"
                msg_location1 = (40+k,100)
                msg_location2 = (210+k,300)
            else:
                msg1 = "Level " + str(self.level)
                msg2 = "PRESS ENTER TO CONTINUE!"
                msg_location1 = (230+k,100)
                msg_location2 = (170+k,300)

            self.MsgRender(self.screen, font, font_size1, msg1, msg_location1,(255,255,255))
            self.MsgRender(self.screen, font, font_size2, msg2, msg_location2,(255,255,255))

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN:
                        self.gamestart = True
                        self.lastFactory = Producer('teddybear',self,assemblerimg[0],0,0)
                        self.factories.add(self.lastFactory)
                        self.lastBeatProgress = self.beatProgress+16
                        if self.health <= 0:
                            self.score = 0
                            self.quota = 200
                            self.level = 1
                        self.health = 100

                if event.type == pygame.QUIT:
                    pygame.display.quit()

            pygame.display.flip()

        while self.gamestart:
            self.scale = 40000.0/self.x_view
            self.x_view += 0.125
            self.y_view += 3/32
            button = False
            if self.health <= 0: # game over
                print("died")
                self.health = -1000
                self.x_view = 200
                self.y_view = self.x_view * 3/4
                self.MainLoop()
                return
            if self.score >= self.quota and self.health > 0:
                self.level += 1
                self.score += self.health
                self.x_view = 200
                self.y_view = self.x_view * 3/4
                self.MainLoop() #TODO: This is actually terrible
                return

            # frameTimeDifference attribute keeps track of the time passed between this frame and the last
            self.frameTimeDifference = clock.tick(120)  #clock.tick also is limiting the frame rate to 60fps

            self.checkEvents()
            self.checkFPS()
            self.trackSongPos()  # smooths out accuracy of song position

            pygame.display.update()  # updates the display to show everything that has been drawn/blit

            # add a factory
            if self.beatProgress-self.lastBeatProgress > 16:
                producer = self.addFactory(self.lastFactory.x, self.lastFactory.y)
                conveyor = Conveyor(producer, self.lastFactory, producer.x, producer.y,self)
                self.lastFactory = producer
                self.lastBeatProgress = self.beatProgress

            # draws sprites onto the screen
            self.screen.fill((20, 20, 20))  # setting background color
            self.allConveyorSprites.draw(self.screen)

            for factory in self.factories.sprites():
                for conveyor in factory.conveyors:
                    self.conveyor_render(self.screen, conveyor)
            self.factories.draw(self.screen)
            for factory in self.factories:
                self.factory_render(self.screen, factory)
                if self.onScreen(factory.x, factory.y):
                    factory.step(pygame.key.get_pressed()[factory.button], self.beatProgress%4-2)
                    if factory.button not in list(self.button_dict.keys()):
                        self.button_dict[factory.button] = factory
                if factory in list(self.button_dict.values()):
                    place = list(self.button_dict.values()).index(factory)
                    self.prod_render(self.screen, factory, place)

            # Display score
            if pygame.font:
                font = pygame.font.Font(None, 40)
                text1 = font.render("Score: %s" % self.score,1,(255,255,0))
                text2 = font.render("Level: %s" % self.level,1,(255,255,0))
                text3 = font.render("Health: %s" % self.health,1,(255,255,0))
                textpos1 = text1.get_rect(top=10, right = self.screen.get_width()-20)
                textpos2 = text2.get_rect(top=50, right = self.screen.get_width()-20)
                textpos3 = text3.get_rect(top=90, right = self.screen.get_width()-20)
                self.screen.blit(text1, textpos1)
                self.screen.blit(text2, textpos2)
                self.screen.blit(text3, textpos3)

            #  displays Debug
            if self.displayDebug:
                self.renderDebug()
Example #24
0
 def __init__(self):
     print "TwitterStreamListener started"
     self.counter = 0
     self.last_sent = time.time()
     self.producer = Producer(host, exchange, routing_type, routing_key)
Example #25
0
    def __init__(self, brokerList, topicName, interval=30):

        self.keys = ["key1", "key2", "key3", "key4", "key5"]
        self.topicName = topicName
        self.Producer = Producer(brokerList)
        self.interval = interval
 def __init__(self, *args, **kwargs):
     Producer.__init__(self, *args, **kwargs)
     self.origin = self.resource
     self.resource = self.q.Queue(maxsize=1)
Example #27
0
def main():
	stack = []
	n = Semaphore(0)
	s = Semaphore(1)
	
	# Create Producer and Consumer threads
	producer1 = Producer(stack, n, s, 1)
	producer2 = Producer(stack, n, s, 2)
	producer3 = Producer(stack, n, s, 3)
	producer4 = Producer(stack, n, s, 4)
	producer5 = Producer(stack, n, s, 5)
	consumer = Consumer(stack, n, s)
	
	# Start all the threads
	producer1.start()
	producer2.start()
	producer3.start()
	producer4.start()
	producer5.start()
	consumer.start()
	
	# Wait for threads to finish
	producer1.join()
	producer2.join()
	producer3.join()
	producer4.join()
	producer5.join()
	consumer.join()
	
	print("Done.")
Example #28
0
                if P < 0:
                    print("That was not a positive integer")
                else:
                    if len([s for s in Processes.Heap
                            if s.Priority == P]) == 0:
                        break
                    else:
                        print(
                            "A process with that priority has already been added"
                        )
        #Create the process with the parameters
        _Process = Process(AT, BT, PID, P)
        #Set process value to it's arrival to be organized by the min-heap
        _Process.value = _Process.ArrivalTime
        Processes.add(_Process)

    os.system('cls')
    #Sets a sleep command to sleep for a second every iteration
    realTime = True if input("Simulate in real time? (y/n):") == "y" else False
    os.system('cls')
    #Create the producer with the processes
    _Producer = Producer(Processes)
    #Start passing process to the consumer based on the scheduling method
    _Producer.Begin(Method, realTime)
    #Output of stats
    _Producer.CPUstats()
    #Can repeatedly run the simulation
    simulate = True if input(
        "Would you like to simulate another set of processes? (y/n):"
    ) == "y" else False
    os.system('cls')
from Producer import Producer

producer = Producer(10, 'Movie trailers', 'Enter your YouTube API key here')
producer.start()
print("Producer has started!")

server = Server()
server.start()
print("Server started")


def broadcast_msg(message):
    for conn in server.all_connected():
        conn.sendMessage(message)


consumer = Consumer(broadcast_msg)
consumer.start()
print("Consumer started")
Example #30
0
class ServerController:
    sensor = None

    def __init__(self):
        '''
        * Quando iniciado, o controller uma instância de subscriber para ouvir as requisições
        * e cria uma instância de publisher para retornar os responses das requisições.
        *  Depois dissoverifica se já existe arquivo de configuração do módulo de leitura,
        * caso positivo, pergunta ao usuário se ele deseja iniciar a conexão com o leitor.
        '''
        self.publisher = Publisher('response')
        self.subscriber = Subscriber('autorama/#')
        self.subscriber.set_topic('autorama/#')

        if os.path.isfile('configs/rfid.json'):
            start_rfid = input(
                f"{bcolors.YELLOW}Arquivo de configuração do RFID existente. Deseja iniciar conexão? (Y/n) {bcolors.COLOR_OFF}"
            )
            if start_rfid == 'Y' or start_rfid == 'y':
                with open('configs/rfid.json', 'r') as file:
                    data = json.load(file)
                    self.__start_connection_rfid(data)
        print('')

    def set_sensor(self, sensor):
        self.sensor = sensor

    # Método com as definições das rotas existentes
    def routes(self, url, body=''):
        url = url.replace('/#', '')

        if body != '':
            try:
                body = json.loads(body)
            except:
                return

        # Condicionais para chamar o método referente a rota chamada
        if url == '/rfid/config':
            response = self.__post_rfid_config(body)
        elif url == '/rfid/tags':
            response = self.__get_rfid_tags(body)
        elif url == '/race/config':
            response = self.__post_race_config(body)
        elif url == '/race/qualification/start':
            response = self.__start_qualification()
        elif url == '/race/start':
            response = self.__start_race()
        else:
            response = '{"status":"NOT_FOUND"}'

        self.publisher.send_message(response.encode("utf-8"), f"response{url}")

    '''
    * Método checa se chegou nova mensagem no tópico de autorama,
    * caso positivo ele chama método de rotas para tratar requisição
    '''

    def check_new_messages(self):
        if self.subscriber.has_new_message():
            body = self.subscriber.get_message()
            route = self.subscriber.get_topic_msg()
            self.routes(route.replace('autorama', ''), body)

    def __post_rfid_config(self, data):
        with open('configs/rfid.json', 'w') as file:
            json.dump(data, file, indent=2)

        self.__start_connection_rfid(data)
        return ''

    def __start_connection_rfid(self, data):
        sensor = Sensor(self, data['serial'], data['baudrate'], data['region'],
                        data['protocol'], data['antenna'], data['frequency'])
        sensor.run()
        if self.sensor != None:
            print(
                f"{bcolors.GREEN}Conexão com RFID iniciado... {bcolors.COLOR_OFF}"
            )

    def __get_rfid_tags(self, data):
        if data['teste'] == None:
            data['teste'] = False
        return self.sensor.get_tags(data['teste'])

    def __post_race_config(self, data):
        self.race = data

        return ''

    def __start_qualification(self):
        self.buffer = __buffer_sensor__()
        self.producer_qualification = Producer(self.buffer, self.sensor,
                                               self.race, 'qualification')
        self.consumer_qualification = Consumer(self.buffer, self.race,
                                               self.publisher, 'qualification')
        self.producer_qualification.start()
        self.consumer_qualification.start()
        return ''

    def __start_race(self):
        self.buffer = __buffer_sensor__()
        self.producer_race = Producer(self.buffer, self.sensor, self.race,
                                      'race')
        self.consumer_race = Consumer(self.buffer, self.race, self.publisher,
                                      'race')
        self.producer_race.start()
        self.consumer_race.start()
        return ''
Example #31
0
    def run(self, announceStartup):

        producerConnectionList = []
        consumerConnectionList = []

        consumer_threads = []
        producer_threads = []

        stats = PrintStats(
            self.samplingInterval, self.latencyLimitation,
            self.producerCount > 0, self.consumerCount > 0,
            ("mandatory" in self.flags or "immediate" in self.flags),
            self.confirm != -1)

        # Create connection
        producer_host, producer_port = self.gethostandportfromuri(self.puri)
        consumer_host, consumer_port = self.gethostandportfromuri(self.curi)

        producer_parameters = pika.ConnectionParameters(
            host=producer_host,
            port=producer_port,
            frame_max=self.frameMax,
            heartbeat_interval=self.heartbeat)

        consumer_parameters = pika.ConnectionParameters(
            host=consumer_host,
            port=consumer_port,
            frame_max=self.frameMax,
            heartbeat_interval=self.heartbeat)

        # support all exchange types
        if self.exchangeType == 'direct':
            # direct
            for i in range(self.consumerCount):
                if announceStartup:
                    print("starting consumer *" + str(i))

                conn = pika.BlockingConnection(parameters=consumer_parameters)
                consumerConnectionList.append(conn)
                consumer_threads.append(
                    self.createConsumer(conn, stats, self.routingKey))

            if self.shouldConfigureQueue():
                conn = pika.BlockingConnection(parameters=consumer_parameters)
                self.configureQueue(conn, self.routingKey)
                conn.close()

            for i in range(self.producerCount):
                if announceStartup:
                    print("starting producer *" + str(i))
                conn = pika.BlockingConnection(parameters=producer_parameters)
                producerConnectionList.append(conn)
                producer_threads.append(
                    self.createProducer(conn, stats, self.routingKey))
        elif self.exchangeType == 'topic':
            # topic
            for i in range(self.consumerCount):
                if announceStartup:
                    print("starting consumer #" + str(i))

                conn = pika.BlockingConnection(parameters=consumer_parameters)
                consumerConnectionList.append(conn)
                consumer_threads.append(
                    self.createConsumer(conn, stats, self.routingPattern))

            if self.shouldConfigureQueue():
                conn = pika.BlockingConnection(parameters=consumer_parameters)
                self.configureQueue(conn, self.routingPattern)
                conn.close()

            for i in range(self.producerCount):
                if announceStartup:
                    print("starting producer #" + str(i))
                conn = pika.BlockingConnection(parameters=producer_parameters)
                producerConnectionList.append(conn)
                producer_threads.append(
                    self.createProducer(conn, stats,
                                        self.routingKey + "." + str(i)))

        elif self.exchangeType == 'fanout':  # uncompleted
            # fanout
            for i in range(self.consumerCount):
                if announceStartup:
                    print("starting consumer *" + str(i))

                conn = pika.BlockingConnection(parameters=consumer_parameters)
                consumerConnectionList.append(conn)
                consumer_threads.append(
                    self.createConsumer(conn, stats, self.routingKey))

            if self.shouldConfigureQueue():
                conn = pika.BlockingConnection(parameters=consumer_parameters)
                self.configureQueue(conn, self.routingPattern)
                conn.close()

            for i in range(self.producerCount):
                if announceStartup:
                    print("starting producer *" + str(i))
                conn = pika.BlockingConnection(parameters=producer_parameters)
                producerConnectionList.append(conn)
                producer_threads.append(
                    self.createProducer(conn, stats, self.routingKey))
        elif self.exchangeType == 'headers':  # uncompleted
            # headers
            for i in range(self.consumerCount):
                if announceStartup:
                    print("starting consumer *" + str(i))

                conn = pika.BlockingConnection(parameters=consumer_parameters)
                consumerConnectionList.append(conn)
                consumer_threads.append(
                    self.createConsumer(conn, stats, self.routingKey))

            for i in range(self.producerCount):
                if announceStartup:
                    print("starting producer *" + str(i))
                conn = pika.BlockingConnection(parameters=producer_parameters)
                producerConnectionList.append(conn)
                producer_threads.append(
                    self.createProducer(conn, stats, self.routingKey))
                producer_threads.append(
                    Producer(conn, self.exchangeName, self.exchangeType,
                             self.routingKey, self.randomRoutingKey,
                             self.flags, self.producerTxSize,
                             self.producerRateLimit, self.producerMsgCount,
                             self.timeLimit, self.minMsgSize, stats))

        # Start Threads
        for i in range(len(consumer_threads)):
            consumer_threads[i].start()
            consumerConnectionList[i].add_timeout(self.timeLimit,
                                                  consumer_threads[i].kill)

        for i in range(len(producer_threads)):
            producer_threads[i].start()

        # Wait Threads finished
        for i in range(len(consumer_threads)):
            consumer_threads[i].join()
            consumerConnectionList[i].close()

        for i in range(len(producer_threads)):
            producer_threads[i].join()
            producerConnectionList[i].close()

        stats.printFinal()
Example #32
0
 def __importProducers(self, df):
     arr = []
     for index, row in df.iterrows():
         arr.append(Producer(index, row))
     return arr
Example #33
0
from Producer import Producer
from StringIO import StringIO
import pycurl

# get the assignment from the assignment url
assignment_buffer = StringIO()
c = pycurl.Curl()
c.setopt(
    c.URL,
    'https://raw.githubusercontent.com/rpfk/python-kafka-producer-assignment/master/assignment.json'
)
c.setopt(c.WRITEDATA, assignment_buffer)
c.perform()
c.close()
assignment = assignment_buffer.getvalue()

# run the producer
Producer('localhost:21', assignment)
Example #34
0
'''
Created on: 03.06.16
@author: Group 6
'''

import sys
from Buffer import Buffer
from Consumer import Consumer
from Producer import Producer
from threading import Thread

#Creates buffer with size given by argument 1
assert (int(sys.argv[1]) > 0), "Buffer is too small"
b = Buffer(int(sys.argv[1]))

producer = {}
consumer = {}

#Creates an amount of consumer set by argument 2
assert (int(sys.argv[2]) > 0), "No Consumer-Thread Running"
for i in range(int(sys.argv[2])):
    consumer[i] = Consumer(b)
    Thread(target=consumer[i].consume).start()

#Creates an amount of producer set by argument 3
assert (int(sys.argv[3]) > 0), "No Producer-Thread Running"
for i in range(int(sys.argv[3])):
    producer[i] = Producer(b)
    Thread(target=producer[i].produce).start()