I have a problem that I don't find how handle it "correctly".
I have this section of code:
interval_available = 5
statusScheduler = 'START'
actualStatus = False
apiKeyList = ['...', '...', '...']
data_variable = [apiKeyList, interval_available, statusScheduler]
while True:
logger.info("OK_01")
client, address = socket.accept()
logger.info("OK_02")
logger.info(f"{address} connected")
tempData = data_variable.copy()
dataString = pickle.dumps(tempData)
sok.send(dataString)
data = client.recv(4096)
data_variable = pickle.loads(data)
client.close()
youtubeList = list()
for i in range(3):
youtubeList.append(build('youtube', 'v3', developerKey=apiKeyList[i]))
youtube = (youtubeList[0], 0)
start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
start_time = start_time[:15] + str(int(start_time[15])+1) + ":00"
if data_variable[0] != apiKeyList:
apiKeyList = data_variable[0]
actualStatus = schedul_shutdown((scheduler01, scheduler02))
if data_variable[1] != interval_available:
interval_available = data_variable[1]
actualStatus = schedul_shutdown((scheduler01, scheduler02))
if data_variable[2] != statusScheduler:
statusScheduler = data_variable[2]
if len(apiKeyList) < 3:
number_total_id = getListVideoId(mydb, count=True)
total_quota = len(apiKeyList)*10000
# interval_available = math.ceil((60*24)/((total_quota*50)/number_total_id))
elif len(apiKeyList) >= 3:
interval_available = 5
if statusScheduler == 'START' and actualStatus == False:
scheduler01 = BackgroundScheduler()
scheduler01.add_job(routine_job, 'interval', args=[mydb, youtube], minutes=interval_available, start_date=start_time)
scheduler01.start()
scheduler02 = BackgroundScheduler()
scheduler02.add_job(check_job, 'interval', args=[mydb, scheduler01], hours=1)
scheduler02.start()
actualStatus = True
elif statusScheduler == 'STOP':
actualStatus = schedul_shutdown((scheduler01, scheduler02))
elif statusScheduler == 'FORCE':
actualStatus = schedul_shutdown((scheduler01, scheduler02), wait=False)
elif statusScheduler == 'PAUSE':
scheduler01.pause()
scheduler02.pause()
elif statusScheduler == 'RESUME':
scheduler01.resume()
scheduler02.resume()
client.close()
socket.close()
I this code seem to work well if we don't look that client, address = socket.accept() is blocking. I want to put all thee socket section in a thread. And if client connect to this program, that socket thread accept and handle the connection (send, work then recv) and pass the data to the main thread (maybe via queue).
But I don't fin how do that. I had checked Thread, Asyncio, Asyncore, non-blocking with Select (seems with Select that work but maybe Thread seems better if well do).
Some can help me. Code, advice, or others... Cordially