def test_initialization_multiple(): l = [] for i in range(0, 10): e = Events() l.append(e) for e in l: e.close()
def test_subscribe_multiple(): pub = Events() sub = Events() time.sleep(0.1) # set up subscriber connection def fn(data): fn.data = data fn.calls += 1 fn.data = None fn.calls = 0 topics = [] for i in range(0, 100): topics.append(str(i)) for topic in topics: sub.connect(fn, topic) for topic in topics: pub.publish(topic, topic) time.sleep(0.1) assert fn.calls == len(topics)
def simple_recv(topic='test_topic', data=None): pub = Events() sub = Events() # set up subscriber connection def fn(data): fn.data = data fn.data = None time.sleep(0.1) sub.connect(fn, topic) time.sleep(0.1) pub.publish(topic, data) time.sleep(0.1) return fn.data
''' Example of sending simple strings using args and kwargs. ''' import time from zmqpc import Events pub = Events() sub = Events() def on_message(*args, **kwargs): print(f'args: {args}\tkwargs: {kwargs}') time.sleep(0.1) sub.connect(on_message, 'apple') for i in range(0, 10): pub.publish('apple', 'arg1', 'arg2', kwarg1='kwarg1', kwarg2='kwarg2', it=i) time.sleep(0.1)
def test_initialization_single(): e = Events() e.close()
''' Example of sending simple strings using args and kwargs. ''' import time from zmqpc import Events pub1 = Events() pub2 = Events() pub3 = Events() pub4 = Events() pub5 = Events() sub = Events() def on_message(*args, **kwargs): print(f'args: {args}\tkwargs: {kwargs}') sub.connect(on_message, 'apple') for i in range(0, 10): pub1.publish('apple', 'pub1') pub2.publish('apple', 'pub2') pub3.publish('apple', 'pub3') pub4.publish('apple', 'pub4') pub5.publish('apple', 'pub5')
''' Example of sending simple strings using args and kwargs. ''' import time from zmqpc import Events pub = Events() sub1 = Events() sub2 = Events() sub3 = Events() sub4 = Events() sub5 = Events() def on_message(*args, **kwargs): print(f'args: {args}\tkwargs: {kwargs}') sub1.connect(on_message, 'apple') sub2.connect(on_message, 'apple') sub3.connect(on_message, 'apple') sub4.connect(on_message, 'apple') sub5.connect(on_message, 'apple') for i in range(0, 10): pub.publish('apple', 'pub1')
''' Example of publishing more kwargs than callback receives. ''' import time from zmqpc import Events pub = Events() sub = Events() def on_message(arg1, arg2, kwarg1=None): print(arg1) print(arg2) print(kwarg1) sub.connect(on_message, 'apple') for i in range(0, 10): pub.publish('apple', 'arg1-aaaa', 'arg2-bbbb', kwarg1='kwarg1-cccc', kwarg2='kwarg2-dddd')
''' Example use of image sending. Sends/receives 100 images. Displays the last image. ''' import os import cv2 import time from pathlib import Path from zmqpc import Events IMGS_TO_PUBLISH = 100 RECV_IMG_NUM = 0 RECV_IMG = None pub = Events() sub = Events() def save_img(img): global RECV_IMG_NUM, RECV_IMG RECV_IMG_NUM += 1 print(f'recv img {RECV_IMG_NUM}/{IMGS_TO_PUBLISH}') RECV_IMG = img time.sleep(0.1) sub.connect(save_img, 'apple') time.sleep(0.1)