Twisted challenge: 'None' returned from imported module
Posted: Wed Dec 27, 2023 8:21 pm
I am getting 'None' as the response as soon as I call the is_operable function, although I tried hard to make is_operable to return a non 'None' response. What am I doing wrong?
This is my app and the response I am getting when I run:
And the output is:
This is the module code:
In this module I chained 3 functions to client.core.get_torrent_status and sent the final response to return_to_caller().
All parameters are passed correctly. What I see when I debug is, the app calls the function in the module, flow is as expected until the line is executed. But then the execution immediately returns to the calling app and the response is 'None'.
How can I prevent 'None' being returned and instead return the output from function_3?
This is my app and the response I am getting when I run:
Code: Select all
from my_module import is_operable
def connect_and_run(**kwargs):
client.connect(host, port, username, password).addCallback(run_after_connecting_to_deluged, **kwargs)
reactor.run()
def run_after_connecting_to_deluged(*args, **kwargs):
kwargs['called_externally'] = True
print('Before calling is_operable')
response = is_operable(*args, **kwargs, torHash='c1463792a1ff36a237e3a0f68badeb0d3764e9bb')
print(f'Response from is_operable is: {response}')
print('After calling is_operable')
Code: Select all
Before calling is_operable
Response from is_operable is: None
After calling is_operable
(here I get other output from is_operable that I expect to get before the "Response from is_operable is..." line
Code: Select all
def is_operable(*args, **kwargs) -> str:
# args is (10,) when the connection is successful.
sys.exit('Connection to deluge daemon was unsuccessful') if not args or args[0] != 10 else None
def return_to_caller(result, **kwargs):
# Expected result is: [(True, 'Function 3 is done')]
print(f'Result is: {result}')
print(kwargs)
if kwargs['called_externally']:
return result
else:
print_n_close(called_externally=False)
def do_the_job(torHash):
d1 = client.core.get_torrent_status(torHash,[])
d2 = d1.addCallback(function_1)
d3 = d2.addCallback(function_2)
d4 = d3.addCallback(function_3)
d4.addCallback(return_to_caller, called_externally=kwargs['called_externally'])
# Call do_the_job and add return_to_caller as a callback to the returned DeferredList
do_the_job(kwargs['torHash'])
reactor.run() if not reactor.running else None
All parameters are passed correctly. What I see when I debug is, the app calls the function in the module, flow is as expected until the line
Code: Select all
d4.addCallback(return_to_caller, ...
How can I prevent 'None' being returned and instead return the output from function_3?