Discord.py Random errors from running bot? - python

This question is mostly code because I have no clue what is happening and how to fix it...
I will also provide the main file. Thanks!
Code (looks something like this; Microsoft Lens is good, but not great) :
import discord
from discord.ext.commands import CommandNotFound
from discord_slash import SlashCommand
def mixedCase(*args) :
total = []
for string in args:
a = map( ''. join,
itertools.product(*((c.upper(), c.lower( ))
for c in string) ) )
for x in list(a):
total.append(x)
return list(total)
client = commands.Bot(case_insensitive=True, command_prefix=mixedCase( "mc" ) )
slash = SlashCommand ( client, sync_commands=True, sync_on_cog_reload=True)
startTime = int(time.time( ))
client.main_startTime = startTime
class MyHelpCommand (commands.MinimalHelpCommand):
async def send_pages(self):
destination = self.get_destination( )
e = discord.Embed(color=discord.Color.green( ), description='')
for page in self.paginator. pages:
e.description += page
e.set_footer (text="Need help? Do 'mcsupport to join the support server!")
await destination.send(embed=e)
client.help_command = MyHelpCommand( )
client.run(TOKEN)
Error:
Traceback (most recent call last):
File "Bot.py", line 127, in <module> client. run( "TOKEN" )
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/client.py", line 723, in run return future. result( )
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/client.py", line 702, in runner await self. start( *args,** kwargs )
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/client.py", line 666, in start await self. connect (reconnect=reconnect)
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/client.py", line 566, in connect await self.ws. poll_event( )
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/gateway.py", line 559, in poll_event await self. received_message(msg. data)
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/gateway.py", line 509, in received_message func (data)
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/state.py", line 490, in parse_message_crea te message = Message(channel=channel, data=data, state=self )
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/message. py", line 584, in _init ref. resolved = self.__class__(channel=chan, data=resol ved, state=state)
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/message.py", line 562, in _init self. stickers = [Sticker(data=data, state=state) for d ata in data. get( 'stickers', [])]
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/message.py", line 562, in <listcomp> self. stickers = [Sticker(data=data, state=state) for d ata in data. get( 'stickers', [])]
File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/sticker.py", line 77, in _init_ self. pack_id = int(data['pack_id'])
KeyError: 'pack_id'
Extracted using Microsoft Lens, I'll try to correct missed words.

Looking at the output and traceback I can see that the error comes from within the discord module. When sticker.py is used, and the class within it is initialized, the pack_id attribute is instantiated with int(data['pack_id']) which should return the value from the dictionary data with key 'pack_id'. It seems that it cannot find such an item. The keyerror suggests that the key 'pack_id' cannot be found in the dictionary data. Usually, data.get('pack_id', 'default value') is better used in order to avoid problems (if value doesn't exist, just replaced with a default value. I am not sure where the issue comes from here, but I don't believe its due to your code.

Related

How to resolve RuntimeError: await wasn't used with future using aiohttp and asyncio in python?

I made some pretty simple script which pulls data from clicky.com api but for some reason it does not work as expected from time to time.
Sometimes it gets results but another time I am getting the following errors which I cant debug. I am fairly new to asyncio and aiohttp
Traceback (most recent call last):
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/1_stable/asy/usable/baza_goals.py", line 118, in <module>
goals_results_last_week = asyncio.run(goals_clicky_results_last_week())
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
return future.result()
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/1_stable/asy/usable/baza_goals.py", line 82, in goals_clicky_results_last_week
responses_clicky_goals = await asyncio.gather(*tasks_goals)
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1122, in send
return self._coro.send(arg)
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/client.py", line 535, in _request
conn = await self._connector.connect(
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 542, in connect
proto = await self._create_connection(req, traces, timeout)
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 907, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 986, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 1040, in create_connection
sock = await self._connect_sock(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 954, in _connect_sock
await self.sock_connect(sock, address)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/selector_events.py", line 502, in sock_connect
return await fut
RuntimeError: await wasn't used with future
How to debug this? Whats the problem here?
edited:
Here is my code for you to test:
import asyncio
import datetime
import aiohttp
import requests
start_operacji = datetime.datetime.now()
print('start', start_operacji)
date_filename = datetime.datetime.now().strftime('%d-%m_%H-%M')
def goals_clicky_tasks_last_week(session):
tasks_clicky_goals = []
# todo to mozna by jeszcze dać do asyncio
clicky_auth = requests.get(
'https://api.clicky.com/api/account/sites?username=meeffe&password=hAs!23$5cy&output=json')
auth_jsonised = clicky_auth.json()
list_site_id_sitekey_dict = []
for k in auth_jsonised:
site_id_sitekey_dict = {'site_id': k['site_id'], 'sitekey': k['sitekey']}
list_site_id_sitekey_dict.append(site_id_sitekey_dict)
for auth_item in list_site_id_sitekey_dict:
goal_url = f"https://api.clicky.com/api/stats/4?site_id={auth_item['site_id']}&sitekey={auth_item['sitekey']}&type=goals&date=today&limit=1000&output=json"
tasks_clicky_goals.append(asyncio.ensure_future(session.get(goal_url, ssl=False)))
return tasks_clicky_goals
async def goals_clicky_results_last_week():
list_final_goals = []
async with aiohttp.ClientSession() as session:
tasks_goals = goals_clicky_tasks_last_week(session)
responses_clicky_goals = await asyncio.gather(*tasks_goals)
for response_clicky_goal in responses_clicky_goals:
clicky_data = await response_clicky_goal.json(content_type=None)
goals_list = []
for url_item_goal in clicky_data[0]['dates'][0]['items']:
if url_item_goal['conversion'] != '':
if url_item_goal['title'].startswith(
'http'): # nie bierze pod uwagę goalsów które zawierają U - https:// etc
goals_dict = {'url': url_item_goal['title'].replace('http://', 'https://'),
'goals': url_item_goal['value'],
'ad_ctr': url_item_goal['conversion']
}
goals_list.append(goals_dict)
else:
continue
else:
continue
list_final_goals.append(goals_list)
flattened_list_final_goals = [val for sublist in list_final_goals for val in sublist]
return flattened_list_final_goals
print(asyncio.run(goals_clicky_results_last_week()), 'goals_clicky_results_last_week')
goals_results_last_week = asyncio.run(goals_clicky_results_last_week())
######################################################################
end = datetime.datetime.now() - start_operacji
print('Ready:)!')
print('It took: ', end)
I actually found a solution by myself.
Instead of aiohttp I used httpx
I used timeout with every request
I removed unnecessary await
Changes in an original code below. Now the script run 100% stable. To be frank I am not sure which of these changes had the biggest impact but it works as expected.
timeout = httpx.Timeout(60.0, connect=60.0)
async with httpx.AsyncClient(verify=False, timeout=timeout) as session:
tasks_goals = goals_clicky_tasks_last_week(session)
responses_clicky_goals = await asyncio.gather(*tasks_goals)
for response_clicky_goal in responses_clicky_goals:
clicky_data = response_clicky_goal.json()
...

how to get posts in a telegram megagroup

I am trying to get the post's text of a megagroup in Telegram using Telethon. I could get the messages from chats however megagroup's posts cannot be retrieved using the same method (How to get channels and groups data from my Telegram account?(Python)). Are megagroups open to fetch their posts by being a simple user using telethon?
Code:
def get_entity_data(entity_id, limit):
entity = client.get_entity(entity_id)
posts = client(GetHistoryRequest(peer=entity, limit=limit, offset_date=None, offset_id=0, max_id=0, min_id=0, add_offset=0, hash=0))
messages = []
for message in posts.messages:
messages.append(message.message)
return messages
result = client(GetDialogsRequest(offset_date=None, offset_id=0, offset_peer=InputPeerEmpty(), limit=100, hash=0)) entities = result.chats entities.reverse()
for entity in entities:
title = entity.title
messages = get_entity_data(entity.id, 10)
print(title + ' :')
print(messages)
print('#######')
and the error message is:
Traceback (most recent call last):
File "./search_message3.py", line 61, in <module>
messages = get_entity_data(entity.id, 10)
File "./search_message3.py", line 48, in get_entity_data
entity = client.get_entity(entity_id)
File "/home/carlos/.local/lib/python3.8/site-packages/telethon/sync.py", line 39, in syncified
return loop.run_until_complete(coro)
File "/home/carlos/.miniconda3/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/home/carlos/.local/lib/python3.8/site-packages/telethon/client/users.py", line 316, in get_entity
chats = (await self(
File "/home/carlos/.local/lib/python3.8/site-packages/telethon/client/users.py", line 30, in __call__
return await self._call(self._sender, request, ordered=ordered)
File "/home/carlos/.local/lib/python3.8/site-packages/telethon/client/users.py", line 84, in _call
result = await future
telethon.errors.rpcerrorlist.PeerIdInvalidError: An invalid Peer was used. Make sure to pass the right peer type and that the value is valid (for instance, bots cannot start conversations) (caused by GetChatsRequest)
Carlos

FastAPI 'put' and 'patch' methods not working and is giving error 500

I am new to FastAPI and I am stuck at fastapi's put method. I am following a certain tutorial.
Here is the code:
#app.put("/blog/{blog_id}", status_code=status.HTTP_202_ACCEPTED)
def update(blog_id: int, request: schemas.Blog,
db: Session = Depends(get_db)):
blog = db.query(models.Blog).filter(models.Blog.id == blog_id)
if blog.first() is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="Blog not found")
blog.update(request, synchronize_session=False)
db.commit()
return 'Blog updated'
#app.patch("/blog/{blog_id}", status_code=status.HTTP_202_ACCEPTED)
def partial_update(blog_id: int, request: schemas.Blog,
db: Session = Depends(get_db)):
blog = db.query(models.Blog).filter(models.Blog.id == blog_id)
if blog.first() is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="Blog not found")
blog.update(request, synchronize_session=False)
db.commit()
return 'Blog updated'
but the thing it is not updating the contents and is giving error 500 if I try to update it.
In console following error is shown:
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/starlette/routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/starlette/routing.py", line 227, in handle
await self.app(scope, receive, send)
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/starlette/routing.py", line 41, in app
response = await func(request)
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/fastapi/routing.py", line 201, in app
raw_response = await run_endpoint_function(
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/fastapi/routing.py", line 150, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
return await loop.run_in_executor(None, func, *args)
File "/usr/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/satvir/Documents/dev/FastApi_Tutorial/./Blog/main.py", line 64, in update
blog.update(request, synchronize_session=False)
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 3204, in update
upd = upd.values(values)
File "<string>", line 2, in values
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/sqlalchemy/sql/base.py", line 106, in _generative
x = fn(self, *args, **kw)
File "<string>", line 2, in values
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/sqlalchemy/sql/base.py", line 135, in check
return fn(self, *args, **kw)
File "/home/satvir/Documents/dev/FastApi_Tutorial/.env/lib/python3.9/site-packages/sqlalchemy/sql/dml.py", line 701, in values
for k, v in arg.items()
AttributeError: 'Blog' object has no attribute 'items'
schemas.Blog:
class Blog(BaseModel):
title: str
content: str
models.Blog:
class Blog(Base):
__tablename__ = "blogs"
id = Column(Integer, primary_key=True, index=True)
title = Column(String)
description = Column(String)
Everything else: creating, retrieving, deleting methods are working fine.
Please help me what to do??
The solution is to change blog.update(request, synchronize_session=False) in your update function to :
blog.update(request.__dict__, synchronize_session=False)
I'll explain why.
From the error message provided, sqlalchemy is attempting to loop over a dictionary, hence it gives that error message when it cannot find a dict type.
The request parameter in blog.update() method is of type Class. To confirm, add this line of code after your if statement:
print(type(request))
You should get an output similar to:
<class 'blog.schemas.Blog'>
Hence, the error. Now do this: print(type(request.__dict__)). Your output should be similar to this: <class 'dict'> which solves the problem.

Parsing error in pyswagger: ValueError: Can't resolve type from:(String, )

I am having an issue with a simple body parsing using pyswagger (library to test OpenAPI compliant APIs).
Here is my script:
import os
from dotenv import load_dotenv, find_dotenv
from pyswagger import App, Security
from pyswagger.contrib.client.requests import Client
# load my security token
load_dotenv(find_dotenv())
api_key = os.environ.get('HELLO_SWAGGER_API_KEY', 'xxx')
hello_swagger_json = 'https://europewest.services.azureml.net/subscriptions/300c8e706bdd414aa1c215ceb2b5a465/services/363818fa1c4a4d0b9d5c03a3dc6f1dfe/swagger.json'
# adapted from pyswagger quick start
app = App._create_(hello_swagger_json)
auth = Security(app)
auth.update_with('api_key', api_key)
client = Client(auth)
# this produces a dictionary exactly as in the schema example for 'execute' operation
hello_body = dict(Inputs=dict(input1=[dict(name="mission-liao")]), GlobalParameters=dict())
# submit execute post request
req, resp = app.op['execute'](body=hello_body)
which give me this error when executing last line:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\spec\v2_0\objects.py", line 283, in __call__
_convert_parameter(final(p))
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\spec\v2_0\objects.py", line 264, in _convert_parameter
c = p._prim_(v, self._prim_factory, ctx=dict(read=False))
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\spec\v2_0\objects.py", line 184, in _prim_
return prim_factory.produce(self.schema, v, ctx) if i == 'body' else prim_factory.produce(self, v, ctx)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\__init__.py", line 200, in produce
val = ret.apply_with(obj, val, ctx)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\_model.py", line 29, in apply_with
self[k] = ctx['factory'].produce(pobj, v)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\__init__.py", line 196, in produce
val = _2nd(obj, ret, val, ctx)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\comm.py", line 40, in _2nd_pass_obj
return ret.apply_with(obj, val, ctx)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\_model.py", line 29, in apply_with
self[k] = ctx['factory'].produce(pobj, v)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\__init__.py", line 196, in produce
val = _2nd(obj, ret, val, ctx)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\comm.py", line 40, in _2nd_pass_obj
return ret.apply_with(obj, val, ctx)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\_array.py", line 48, in apply_with
self.extend(map(functools.partial(ctx['factory'].produce, obj.items), val))
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\__init__.py", line 200, in produce
val = ret.apply_with(obj, val, ctx)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\_model.py", line 29, in apply_with
self[k] = ctx['factory'].produce(pobj, v)
File "P:\Development\Anaconda3\envs\tgml36\lib\site-packages\pyswagger\primitives\__init__.py", line 192, in produce
raise ValueError('Can\'t resolve type from:(' + str(obj.type) + ', ' + str(obj.format) + ')')
ValueError: Can't resolve type from:(String, )
I cannot really understand if there is something I am doing wrong or if this is a bug.
This is actually a very simple example I was trying to build because I am having an issue with authentication in a more complex example, but while in the more complex example the body parsing does not raise errors, here I am having problems even before sending a request!
The script should be reproducible (swagger.json should be accessible from the url in the script, if not I can attach it) and I have logging info only when running the command app = App._create_(hello_swagger_json) (so this too should be reproducible too using the above script).
The webservice that I am trying to test is a simple Web service built with Azure ML Studio (which provides a Swagger API, the reason why I found out about Swagger in the first place).
I am running with pyswagger version 0.8.39.

Unable to make high number of posts on remote machine vs local using asynio and aiohttp

I wrote a program that would post events using asyncio and aiohttp. This program works when I run it locally. I can post 10k events no problem. However, I SCPed the whole codebase to a remote machine and within that machine I can't post more than 15 events without getting this error:
RuntimeError: Event loop is closed
Exception ignored in: <coroutine object Poster.async_post_event at 0x7f4a53989410>
Traceback (most recent call last):
File "/home/bli1/qe-trinity/tracer/utils/poster.py", line 63, in async_post_event
File "/home/bli1/py/python3.5/lib/python3.5/site-packages/aiohttp/client.py", line 565, in __aenter__
File "/home/bli1/py/python3.5/lib/python3.5/site-packages/aiohttp/client.py", line 198, in _request
File "/home/bli1/py/python3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 316, in connect
File "/home/bli1/py/python3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 349, in _release_waiter
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/futures.py", line 332, in set_result
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/futures.py", line 242, in _schedule_callbacks
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/base_events.py", line 447, in call_soon
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/base_events.py", line 456, in _call_soon
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/base_events.py", line 284, in _check_closed
RuntimeError: Event loop is closed
Exception ignored in: <coroutine object Poster.async_post_event at 0x7f4a5397ffc0>
Traceback (most recent call last):
File "/home/bli1/qe-trinity/tracer/utils/poster.py", line 63, in async_post_event
File "/home/bli1/py/python3.5/lib/python3.5/site-packages/aiohttp/client.py", line 565, in __aenter__
File "/home/bli1/py/python3.5/lib/python3.5/site-packages/aiohttp/client.py", line 198, in _request
File "/home/bli1/py/python3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 316, in connect
File "/home/bli1/py/python3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 349, in _release_waiter
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/futures.py", line 332, in set_result
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/futures.py", line 242, in _schedule_callbacks
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/base_events.py", line 447, in call_soon
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/base_events.py", line 456, in _call_soon
File "/home/bli1/py/python3.5/lib/python3.5/asyncio/base_events.py", line 284, in _check_closed
RuntimeError: Event loop is closed
How can I debug this or find out the source of this problem?
Here is the class that I created and I use the method post() to run:
import uuid
import os
import asyncio
import time
import random
import json
import aiohttp
from tracer.utils.phase import Phase
class Poster(Phase):
def __init__(self, log, endpoint, num_post, topic, datafile, timeout, oracles, secure=False, thru_proxy=True):
Phase.__init__(self, log, "post", oracles, secure, thru_proxy)
self.log = log
self.num_post = int(num_post)
self.datafile = datafile.readlines()
self.topic = topic
self.endpoint = self.set_endpoint(endpoint, self.topic)
self.response = None
self.timeout = timeout
def random_line(self):
""" Returns random line from file and converts it to JSON """
return json.loads(random.choice(self.datafile))
#staticmethod
def change_uuid(event):
""" Creates new UUID for event_id """
new_uuid = str(uuid.uuid4())
event["event_header"]["event_id"] = new_uuid
return event
#staticmethod
def wrapevent(event):
""" Wrap event with metadata for analysis later on """
return {
"tracer": {
"post": {
"statusCode": None,
"timestamp": None,
},
"awsKafkaTimestamp": None,
"qdcKakfaTimestamp": None,
"hdfsTimestamp": None
},
"event": event
}
def gen_random_event(self):
random_event = self.random_line()
event = self.change_uuid(random_event)
dataspec = self.wrapevent(event)
return dataspec
async def async_post_event(self, event, session):
async with session.post(self.endpoint, data=event, proxy=self.proxy) as resp:
event["tracer"]["post"]["timestamp"] = time.time() * 1000.0
event["tracer"]["post"]["statusCode"] = resp.status
unique_id = event["event"]["event_header"]["event_id"]
oracle_endpoint = os.path.join(self.oracle, unique_id)
async with session.put(oracle_endpoint, data=json.dumps(event), proxy=self.proxy) as resp:
if resp.status != 200:
self.log.debug("Post to ElasticSearch not 200")
self.log.debug(event["event"]["event_header"]["event_id"])
self.log.debug("Status code: " + str(resp.status))
return event["event"]["event_header"]["event_id"], resp.status
async def async_post_events(self, events):
coros = []
conn = aiohttp.TCPConnector(verify_ssl=self.secure)
async with aiohttp.ClientSession(connector=conn) as session:
for event in events:
coros.append(self.async_post_event(event, session))
return await asyncio.gather(*coros)
def post(self):
event_loop = asyncio.get_event_loop()
try:
events = [self.gen_random_event() for i in range(self.num_post)]
start_time = time.time()
results = event_loop.run_until_complete(self.async_post_events(events))
print("Time taken: " + str(time.time() - start_time))
finally:
event_loop.close()
You cannot re-use a loop once it's closed. From AbstractEventLoop.close documentation:
This is idempotent and irreversible. No other methods should be called after this one.
Either remove the loop.close call or create a new loop for each post.
My advice would be to avoid those problems by running everything inside the loop and awaiting async_post_events when needed.

Categories