Код завершается, когда я запускаю следующий код
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")
Код завершается в блокноте jupyter
Но когда я пытаюсь заполнить очередь, даже после удаления переменной, онависнет.
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")
виснет навсегда.Даже создание новой очереди не помогает.Куда я иду не так.Пожалуйста, ведите меня.
Заранее спасибо.
Я использую Python 3.