Python asyncio event loop

Viewed 26

I want to do some commands on very high requests like 10000 and above, but even when I do for like 100 commands, it blocks some of them and if I insert 100 values, 99 are added. When I add 1000 values 15 are missing and it gives the below error

import aiomysql
import asyncio
import logging
import random
import time


logging.basicConfig(level=logging.DEBUG)
print(f'Logging Enabled: Functions being defiled')
async def select(loop, sql, pool):
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute(sql)
            # r = await cur.fetchone()
            # print(r)


async def insert(loop, sql, pool):
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute(sql)
            await conn.commit()

async def update(loop, sql, pool):
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:

            await cur.execute(sql)
            await conn.commit()




async def main(loop):
    start = time.time()
    tasks = []
    print(f'Def Main Looped Called: Mysql being tried to Login ')
    pool = await aiomysql.create_pool(
        host='localhost',
        user='root',
        password='',
        db='mydatabase',
        loop=loop,
        minsize=50,
        maxsize=500)
    for x in range(100):
        list = random.sample(range(0, 9), 7)
        digit1 = random.sample(range(1, 6), 1)
        digit2 = random.sample(range(0, 9), 1)
        netno = random.sample(range(1, 4), 1)
        netno2 = random.sample(range(1, 4), 1)
        substatus = random.sample(range(1, 3), 1)
        output = "".join(str(i) for i in list)
        output1 = "".join(str(i) for i in digit1)
        output2 = "".join(str(i) for i in digit2)
        netoutput = "".join(str(i) for i in netno)
        netoutput2 = "".join(str(i) for i in substatus)
        netoutput3 = "".join(str(i) for i in netno2)
        final = ('923' + output1 + output2 + output)

        c1 = select(loop=cur_loop, sql="SELECT DISTINCT id, msisdn, network_type, network_name, subscription_status, package_id \
               FROM testdata JOIN networkstatus ON testdata.network_type=networkstatus.network_id \
               WHERE network_type=" +netoutput+ " AND subscription_status= " +netoutput2+ " \
               AND package_id=" +netoutput3+ ";", pool=pool)
        endtimes=time.time()

        c2 = insert(loop=cur_loop, sql="INSERT INTO testdata (msisdn,network_type,subscription_status,package_id) \
         VALUES (" +final+ "," +netoutput+ "," +netoutput2+ "," +netoutput+ ")", pool=pool)
        endtimei=time.time()

        c3 = update(loop=cur_loop, sql="UPDATE testdata SET network_type = " +netoutput+ ", subscription_status=  " +netoutput2+ ",\
         package_id = " +netoutput+ "  WHERE network_type=" +netoutput+ " AND subscription_status= " +netoutput2+ " \
         AND package_id=" +netoutput+ ";", pool=pool)
        endtimeu=time.time()

        print(f' Sql Connected Main Function Ended')
        print('Time taken by select:', endtimes-start, 'Time taken by insert:', endtimei-start, 'Time taken by update:', endtimeu-start)
        # await c1,c2,c3
        tasks = [asyncio.ensure_future(c1), asyncio.ensure_future(c2), asyncio.ensure_future(c3)]
        print(f'Task Line Executed')
    end = time.time()
    print('Total time of execution:', end-start)
    return await asyncio.gather(*tasks)



if __name__ == '__main__':
    print(f'Code Started: Async Event Loop')
    cur_loop = asyncio.get_event_loop()
    asyncio.set_event_loop(cur_loop)
    print(f'Before Loop Run')
    cur_loop.run_until_complete(main(cur_loop))

it select update and insert values but not complete, some values are missing as it gives the error

0 Answers
Related