I would just rewrite it to not try print a function with callbacks in, or if need do like that, then I believe need maybe a deferred or alike, never used that myself, but easy look up I believe. Note not very good at this personally btw, and only used simple callBacks, callLaters and loopingCalls from twisted, and so not really the best qualified for answering this, but still.
This will happen even without chained callbacks(so just e.g. d1), I believe, as you cannot capture/print a callback like that, and so returns none per spec when no return-value. Btw, if neither getting later results correctly neither, then must be related to your non-posted function_*'s.
If i'm way off, then apologies in advance for noise
Edit: Just in case I didn't explain properly, then this single similar callback for debian netinst iso returns None and thereafter the actual result, like yours:
Code: Select all
from deluge.ui.client import client
from twisted.internet import reactor
def a(*args,**kwargs):
def b(c):
print(c)
return c
#client.disconnect
#reactor.stop()
def foo():
client.core.get_torrent_status('6d4795dee70aeb88e03e5336ca7c9fcf0a1e206d',[]).addCallback(b)
x = foo()
print(x)
client.connect().addCallback(a)
reactor.run()
Showing:
Code: Select all
martin@arch ~/Downloads % python deluge-client-test.py
None
{'active_time': 34314, 'seeding_time': 34292, 'finished_time': 34292, 'all_time_download': 422889418, 'storage_mode': 'sparse', 'distributed_copies': 1.0, 'download_payload_rate': 0, 'file_priorities': (1,), 'hash': '6d4795dee70aeb88e03e5336ca7c9fcf0a1e206d', 'auto_managed': True, 'is_auto_managed': True, 'is_finished': True, 'max_connections': -1, 'max_download_speed': -1, 'max_upload_slots': -1, 'max_upload_speed': -1, 'message': 'OK', 'move_on_completed_path': '/home/martin/Downloads', 'move_on_completed': False, 'move_completed_path': '/home/martin/Downloads', 'move_completed': False, 'next_announce': 596, 'num_peers': 1, 'num_seeds': 0, 'owner': 'localclient', 'paused': False, 'prioritize_first_last': False, 'prioritize_first_last_pieces': False, 'sequential_download': False, 'progress': 100.0, 'shared': False, 'remove_at_ratio': True, 'save_path': '/home/martin/Downloads', 'download_location': '/home/martin/Downloads', 'seeds_peers_ratio': 235.0, 'seed_rank': 8, 'state': 'Seeding', 'stop_at_ratio': True, 'stop_ratio': 0.5, 'time_added': 1702664476, 'total_done': 406847488, 'total_payload_download': 0, 'total_payload_upload': 0, 'total_peers': 1, 'total_seeds': 235, 'total_uploaded': 0, 'total_wanted': 406847488, 'total_remaining': 0, 'tracker': 'http://bttracker.debian.org:6969/announce', 'tracker_host': 'debian.org', 'trackers': ({'url': 'http://bttracker.debian.org:6969/announce', 'trackerid': '', 'tier': 0, 'fail_limit': 0, 'source': 1, 'verified': False, 'message': '', 'last_error': {'value': 0, 'category': ''}, 'next_announce': None, 'min_announce': None, 'scrape_incomplete': 0, 'scrape_complete': 0, 'scrape_downloaded': 0, 'fails': 0, 'updating': False, 'start_sent': False, 'complete_sent': False, 'endpoints': (), 'send_stats': False},), 'tracker_status': 'Announce OK', 'upload_payload_rate': 0, 'comment': '"Debian CD from cdimage.debian.org"', 'creator': 'mktorrent 1.1', 'num_files': 1, 'num_pieces': 1552, 'piece_length': 262144, 'private': False, 'total_size': 406847488, 'eta': 0, 'file_progress': (1.0,), 'files': ({'index': 0, 'path': 'debian-11.6.0-amd64-netinst.iso', 'size': 406847488, 'offset': 0},), 'orig_files': ({'index': 0, 'path': 'debian-11.6.0-amd64-netinst.iso', 'size': 406847488, 'offset': 0},), 'is_seed': True, 'peers': (), 'queue': -1, 'ratio': 0.0, 'completed_time': 1702664498, 'last_seen_complete': 1703789344, 'name': 'debian-11.6.0-amd64-netinst.iso', 'pieces': None, 'seed_mode': False, 'super_seeding': False, 'time_since_download': 1126014, 'time_since_upload': -1, 'time_since_transfer': 1126014}
I tested your code, like this, and same if not adding function_1 and just using single callback(d1), like above:
Code: Select all
from deluge.ui.client import client
from twisted.internet import reactor
def connect_and_run(**kwargs):
client.connect().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='6d4795dee70aeb88e03e5336ca7c9fcf0a1e206d')
print(f'Response from is_operable is: {response}')
print('After calling is_operable')
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):
def function_1(result,**kwargs):
return result
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'])
d2.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
connect_and_run()
Showing:
Code: Select all
martin@arch ~/Downloads % python test2.py
Before calling is_operable
Response from is_operable is: None
After calling is_operable
Result is: {'active_time': 34862, 'seeding_time': 34840, 'finished_time': 34840, 'all_time_download': 422889418, 'storage_mode': 'sparse', 'distributed_copies': 1.0, 'download_payload_rate': 0, 'file_priorities': (1,), 'hash': '6d4795dee70aeb88e03e5336ca7c9fcf0a1e206d', 'auto_managed': True, 'is_auto_managed': True, 'is_finished': True, 'max_connections': -1, 'max_download_speed': -1, 'max_upload_slots': -1, 'max_upload_speed': -1, 'message': 'OK', 'move_on_completed_path': '/home/martin/Downloads', 'move_on_completed': False, 'move_completed_path': '/home/martin/Downloads', 'move_completed': False, 'next_announce': 48, 'num_peers': 0, 'num_seeds': 0, 'owner': 'localclient', 'paused': False, 'prioritize_first_last': False, 'prioritize_first_last_pieces': False, 'sequential_download': False, 'progress': 100.0, 'shared': False, 'remove_at_ratio': True, 'save_path': '/home/martin/Downloads', 'download_location': '/home/martin/Downloads', 'seeds_peers_ratio': 235.0, 'seed_rank': 8, 'state': 'Seeding', 'stop_at_ratio': True, 'stop_ratio': 0.5, 'time_added': 1702664476, 'total_done': 406847488, 'total_payload_download': 0, 'total_payload_upload': 0, 'total_peers': 1, 'total_seeds': 235, 'total_uploaded': 0, 'total_wanted': 406847488, 'total_remaining': 0, 'tracker': 'http://bttracker.debian.org:6969/announce', 'tracker_host': 'debian.org', 'trackers': ({'url': 'http://bttracker.debian.org:6969/announce', 'trackerid': '', 'tier': 0, 'fail_limit': 0, 'source': 1, 'verified': False, 'message': '', 'last_error': {'value': 0, 'category': ''}, 'next_announce': None, 'min_announce': None, 'scrape_incomplete': 0, 'scrape_complete': 0, 'scrape_downloaded': 0, 'fails': 0, 'updating': False, 'start_sent': False, 'complete_sent': False, 'endpoints': (), 'send_stats': False},), 'tracker_status': 'Announce OK', 'upload_payload_rate': 0, 'comment': '"Debian CD from cdimage.debian.org"', 'creator': 'mktorrent 1.1', 'num_files': 1, 'num_pieces': 1552, 'piece_length': 262144, 'private': False, 'total_size': 406847488, 'eta': 0, 'file_progress': (1.0,), 'files': ({'index': 0, 'path': 'debian-11.6.0-amd64-netinst.iso', 'size': 406847488, 'offset': 0},), 'orig_files': ({'index': 0, 'path': 'debian-11.6.0-amd64-netinst.iso', 'size': 406847488, 'offset': 0},), 'is_seed': True, 'peers': (), 'queue': -1, 'ratio': 0.0, 'completed_time': 1702664498, 'last_seen_complete': 1703789344, 'name': 'debian-11.6.0-amd64-netinst.iso', 'pieces': None, 'seed_mode': False, 'super_seeding': False, 'time_since_download': 1126562, 'time_since_upload': -1, 'time_since_transfer': 1126562}
{'called_externally': True}
The 'result' was just for testing, know you want otherwise from your function_3 on your end.