I am trying to create a local server which listens indefinitely and provides the latest information. The server runs another while loop which fetches the latest info every one hour. I have tried multi-threading approach, in which one thread is a socket server (runs indefinitely) and another thread (runs indefinitely with sleep) updates the latest info to a global variable.The global variable is then accesses by the server and is sent to it's clients. However I see that only one thread is working at a time and the other thread isn't kicking-off at all. Following is the code. (Please note that calling getInfo()/retriveInfoFromDB() to fetch latest info every time a client request is made is not desired as the former is very time consuming process) .Any help is much appreciated.
from socket import *
import threading
import pandas as pd
import json
import time
import datetime
import thread
#Global Info variable
info = pd.DataFrame()
def getInfo():
global info
while 1:
print "Information retrieval running"
try:
#This will bring the required infor as pandas df
info = retriveInfoFromDB()
except:
pass
time.sleep(3600)
def runServer():
global info
address = ('localhost', 6005)
server_socket = socket(AF_INET, SOCK_DGRAM)
server_socket.bind(address)
while(1):
print "Listening"
recv_data, addr = server_socket.recvfrom(2048)
server_socket.sendto(info.to_json(path_or_buf = None, orient = 'records', date_format = 'epoch', double_precision = 10, force_ascii = True, date_unit = 'ms', default_handler = None), addr)
th1 = threading.Thread(target=runServer())
th2 = threading.Thread(target=getInfo())
th1.start()
th2.start()
You're passing the result of calling runServer()
as a parameter, not the function itself. So the next line (thread) is never processed. Remove the parenthesis when passing the functions as arguments
th1 = threading.Thread(target=runServer)
th2 = threading.Thread(target=getInfo)
There are lots of other commentaries on your code, but this will get you going.