Git Product home page Git Product logo

azureeventhubapi's Introduction

Azure Eventhub python wrapper

This wrapper will publish messages to the azure eventhub. IT supports both sync and async mode of publishing messages

To install

pip install eventhub_wrapper

or from source:

pip install <path to event_hub directory>

Features!

The eventhub configuration is read from a default configuration file named credentials.json.

Sample credentials.json:

{
	"EVENTSHUB_ACCOUNT_NAME": "<eventhubs account name>",
	"EVENTSHUB_NAME": "<eventshub name>",
	"EVENTSHUB_CONNECTION_STRING":"Endpoint=sb://xxxxxxxxxxxx.servicebus.windows.net/;SharedAccessKeyName=xxxxxxxxxx;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

Sample Async Publisher

import time
import asyncio
import eventhub_wrapper.asynchronous.eventhub_async_wrapper as wrapper
import eventhub_wrapper.asynchronous.log_async_publisher as log_async_publisher

'''
        This is a samnple program demonstrating how to publish to events hub using the async wrapper
        the program publishes 1000 messages
        the messages are simple string
         
         Parameters
         ----------
         producer: the eventhub async client

         Returns
         -------
         none
'''

async def run(producer):
    async with producer:
        for i in range(1000):
            await wrapper.send_event_data_batch(producer, "testing {}".format(i))
            if i % 100 == 0:
                print("{}".format(i))


if __name__ == '__main__':

    ### use the utility setup method to create a producer (publisher) object

    producer = log_async_publisher.eventhub_setup()

    ### since the producer is async - an eventloop is needed to ensure are tasks are complete before exiting.

    loop = asyncio.get_event_loop()

    #### performance measure -
    start_time = time.time()

    #### the loop ensures all messages are published.  in the production code, run_forever() may need to be used
    loop.run_until_complete(run(producer))
    print("Send messages in {} seconds.".format(time.time() - start_time))

Sample Sync Publisher

import time

import eventhub_wrapper.synchronous.log_publisher as log_publisher

'''
        This is a samnple program demonstrating how to publish to events hub using the sync wrapper
        the program publishes 1000 messages
        the messages are simple string

         Parameters
         ----------
        none
         Returns
         -------
        none
'''
if __name__ == '__main__':
    # performance measure.
    start_time = time.time()
    # using the utility function, an eventshub object is created in sync mode
    producer = log_publisher.eventhub_setup()
    # printing the events hub properties.  if not connected, this method with throw an exception
    print("event publisher properties are:", producer.get_eventhub_properties())

    # publish a 1000 messages of simple string
    with producer:
        for i in range (1000):
            log_publisher.publish_event(producer, 1, "testing {}".format(i))
            if i % 10 == 0:
                print("{}".format(i))

    print("Send messages in {} seconds.".format(time.time() - start_time))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.