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))