0

I have got an XMLRPC server and client runs some functions on server and gets returned value. If the function executes quickly then everything is fine but I have got a function that reads from file and returns some value to user. Reading takes about minute(there is some complicated stuff) and when one client runs this function on the server then server is not able to respond for other users until the function is done.

I would like to create new thread that will read this file and return value for user. Is it possible somehow?

Are there any good solutions/patters to do not block server when one client run some long function?

Mateusz
  • 604
  • 8
  • 20
  • What are you using for your XMLRPC server? DId you write it yourself or are you using a library? – ErikR Jul 24 '16 at 13:20

2 Answers2

0

Yes it is possible , this way

#starting the thread
def start_thread(self):
     threading.Thread(target=self.new_thread,args=()).start()

# the thread you are running your logic
def new_thread(self, *args):
    #call the function you want to retrieve data from
    value_returned = partial(self.retrieved_data_func,arg0)

#the function that returns
def retrieved_data_func(self):
    arg0=0
    return arg0
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ronald Saunfe
  • 561
  • 6
  • 20
-2

Yes, using the threading module you can spawn new threads. See the documentation. An example would be this:

import threading
import time

def main():
    print("main: 1")
    thread = threading.Thread(target=threaded_function)
    thread.start()
    time.sleep(1)
    print("main: 3")
    time.sleep(6)
    print("main: 5")

def threaded_function():
    print("thread: 2")
    time.sleep(4)
    print("thread: 4")

main()

This code uses time.sleep to simulate that an action takes a certain amount of time. The output should look like this:

main: 1
thread: 2
main: 3
thread: 4
main: 5
Dartmouth
  • 1,069
  • 2
  • 15
  • 22