0

I am trying to call a function from a thread. The threaded function takes a function as a parameter in the main function call of the thread.

 Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
TypeError: 'dict' object is not callable

Above is the error I am receiving.. When i run it not being in a thread ie request_() rather than request() works but for some reason when used in a thread it cant return the value properly. Also if I remove the return statement in req() it works in the thread. But I want to have the returned value of the function used as a parameter in the main threaded function. Any help appreciated :)

def req():
        r = requests.get('http://url')
        return json.loads(r.content)   
    
    class limiter:
    
    
        def __init__(self, interval, allowed):
            if allowed < 1:
                exit()
            self.connections_ = 0
            self.time_created_ = time.time()
            self.request_interval_ = interval
            self.requests_allowed = allowed
    
        
    
        def request_(self, function):
            res = None
            if time.time() < (self.time_created_ + self.request_interval_):
                if self.connections_ < self.requests_allowed:
                    self.connections_ += 1
                    res = function()
                else:
                    while self.connections_ > self.requests_allowed:
                        if time.time() > (self.time_created_ + self.request_interval_):
                           break 
                        continue
                    self.connections_ += 1
                    res = function()
            
            if time.time() > (self.time_created_ + self.request_interval_):
                self.connections_ = 0
                self.time_created_ = time.time()
            return res
            
    
        
        def request(self, function):
            p1 = Process(target=self.request_(function))
            p1.start()
            p1.join()
    
    l = limiter(10, 10)
    print(l.request(req))

1 Answers1

0

You pass the result of your method as Process target instead of the method itself.

You want to do:

p1 = Process(target=self.request_, args=(function,))
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
  • That works! thanks Sven.. one thing now the thread function request() returns None but if i print out the response before return res it has the correct value. I dont know if you know this but is it possible to get a usable returned value from a thread how im trying? – meanthatmuchtoyou Jun 19 '21 at 23:47
  • Yes, but not directly from the standard `Thread` or `Process` instance. There are a lot of ways to achieve this. See [How to get the return value from a thread in python?](https://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python). Read through this. If you have still any questions, please feel free to ask again. – Sven Eberth Jun 20 '21 at 00:23
  • will do appreciate that – meanthatmuchtoyou Jun 20 '21 at 00:44
  • 1
    ended up getting it going Sven with the help of that thread.. thanks again!! – meanthatmuchtoyou Jun 25 '21 at 16:06