Skip to content

Instantly share code, notes, and snippets.

@avilior
Created December 9, 2019 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avilior/fd62fb88bf44d5d8fc84511ffdffa517 to your computer and use it in GitHub Desktop.
Save avilior/fd62fb88bf44d5d8fc84511ffdffa517 to your computer and use it in GitHub Desktop.
Basic asyncio base template with python 3.8
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import asyncio
import logging
import uvloop
from kafka_producer import KafkaProducer
from kafka_consumer import KafkaConsumer
LOG=logging.getLogger(__name__)
async def some_coroutine():
while True:
LOG.debug(F"Doing something then sleeping")
await asyncio.sleep(0)
LOG.info("Coroutine is done")
async def main():
global kafkaProducer4TestTopic, kafkaConsumer4TestTopic
LOG.info("Main starting")
loop = asyncio.get_event_loop()
tasks = []
tasks.append(asyncio.create_task(some_coroutine()))
await asyncio.gather(*tasks)
LOG.info("Main ending")
if __name__ == '__main__':
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s.%(msecs)03d|%(name)-20s|%(lineno)4d|%(levelname)-8s|%(message)s',
datefmt='%m-%d %H:%M:%S')
uvloop.install() # make uvloop available
try:
asyncio.run(main(), debug=False)
except KeyboardInterrupt:
print("Keyboard interruption...")
except Exception as x:
LOG.exception(F"Other exception {x}")
raise
print("We are done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment