May-21-2018, 11:25 AM
The code is getting exited when I run the following code
But when I try to repopulate the queue even after removing the variable it hangs.
Thanks in advance.
I am using Python 3.
import mysql.connector import mysql.connector.pooling as x import time from threading import Thread from queue import Queue dbconfig = { "database": "mydb", "user": "root", "password": "rootpass", "host":"127.0.0.1" } cnxpool =x.MySQLConnectionPool(pool_name = "mypool",pool_size = 2,**dbconfig) q = Queue(maxsize=20) threads = [] limit = 0 fetch_size = 20 flag = True def populate(): cnx = cnxpool.get_connection() cursor = cnx.cursor() global flag,fetch_size,limit while flag: cursor.execute("SELECT mycol from mytab limit " + str(limit)+","+ str(fetch_size)) rows = cursor.fetchall() for row in rows: print("Putting "+ str(row[0])) q.put(row[0]) if cursor.rowcount < 20 : for i in range(4): q.put(None) flag = False limit = limit + 20 cursor.close() cnx.close() def consume(): while True: item = q.get() if item is None: q.task_done() break time.sleep(1) print(item) q.task_done() for i in range(4): t = Thread(target=consume) t.start() threads.append(t) y = Thread(target=populate) y.start() q.join() y.join() for t in threads: t.join() print("Completed")Code is getting completed in jupyter notebook
But when I try to repopulate the queue even after removing the variable it hangs.
import mysql.connector import mysql.connector.pooling as x import time from threading import Thread from queue import Queue dbconfig = { "database": "mydb", "user": "root", "password": "rootpass", "host":"127.0.0.1" } cnxpool =x.MySQLConnectionPool(pool_name = "mypool",pool_size = 2,**dbconfig) q = Queue(maxsize=20) threads = [] limit = 0 fetch_size = 20 flag = True def populate(): cnx = cnxpool.get_connection() cursor = cnx.cursor() global flag,fetch_size,limit while flag: cursor.execute("SELECT mycol from mytab limit " + str(limit)+","+ str(fetch_size)) rows = cursor.fetchall() for row in rows: print("Putting "+ str(row[0])) q.put(row[0]) if cursor.rowcount < 20 : for i in range(4): q.put(None) flag = False limit = limit + 20 cursor.close() cnx.close() def consume(): while True: item = q.get() if item is None: q.task_done() break time.sleep(1) print(item) q.task_done() for i in range(4): t = Thread(target=consume) t.start() threads.append(t) y = Thread(target=populate) y.start() q.join() y.join() for t in threads: t.join() print("Completed") del q q = Queue(maxsize=20) for i in range(4): t = Thread(target=consume) t.start() threads.append(t) y = Thread(target=populate) y.start() q.join() y.join() for t in threads: t.join() print("Completed")It hangs for ever. Even creating a new queue doesn't help. Where am I going wrong. Kindly guide me.
Thanks in advance.
I am using Python 3.