Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

How do I make code save local variables between its processes in python like ipython notebook does?

I think my title isn't clear so... I made this code which fetches top Dota TV games as an array of these match_ids and prints them in the end. STEAM_LGN, STEAM_PSW are steam login/password combination.

from steam.client import SteamClient
from dota2.client import Dota2Client

client = SteamClient()
dota = Dota2Client(client)

@client.on('logged_on')
def start_dota():
    dota.launch()

match_ids = []
@dota.on('top_source_tv_games')
def response(result):
    for match in result.game_list: # games
        match_ids.append(match.match_id)  
    
def request_matches():
    dota.request_top_source_tv_games()

client.cli_login(username=STEAM_LGN, password=STEAM_PSW)

request_matches()
dota.on('top_source_tv_games', response)
print(match_ids)

The thing I'm having a problem with

When using Anaconda iPython Notebook -> when I run the cell for the first time -> it returns me

[]

but when I do it for the second time, it returns me a real data, for example

[5769568657, 5769554974, 5769555609, 5769572298, 5769543230, 5769561446, 5769562113, 5769552763, 5769550735, 5769563870]

So every time when I am playing in my ipython notebook sandbox -> I hit Shift+Enter twice and get the data.

But now I need to tranfer this code to a bigger project. So for example let's say I save that code to dota2.info.py file and have this code in another file referencing dota2.info.py:

import subprocess
import time 

### some code ###

while(True):
    subprocess.call(['python', './dota2info.py'])
    time.sleep(10)

And when running project code this always prints [] like it was doing on first Shift+Enter cell-running in Anaconda ipython notebook.

[]
[] 
[]
...

So my question is what should I do in this situation ? How can I solve this problem of (I don't know) ValvePython/dota2 code caching some important data in local unknown to me variables in ipython notebook?

Ideally I want the code immediately give me real data without having these [].

question from:https://stackoverflow.com/questions/65545768/how-do-i-make-code-save-local-variables-between-its-processes-in-python-like-ipy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Hard to tell why it happens, but as a possible workaround I'd try wrapping the code in the cell you're re-running, in a function that retries until nonempty results are achieved.

For example, assuming everything was in the rerun cell except the imports, this might be dota2info.py:

from steam.client import SteamClient
from dota2.client import Dota2Client
import time

def get_results():
    client = SteamClient()
    dota = Dota2Client(client)

    @client.on('logged_on')
    def start_dota():
        dota.launch()

    match_ids = []
    @dota.on('top_source_tv_games')
    def response(result):
        for match in result.game_list: # games
            match_ids.append(match.match_id)  
        
    def request_matches():
        dota.request_top_source_tv_games()

    client.cli_login(username=STEAM_LGN, password=STEAM_PSW)

    request_matches()
    dota.on('top_source_tv_games', response)
    return match_ids

if __name__ == "__main__":
    results = get_results()
    max_retries = 10
    retries = 0
    while not results and retries < max_retries:
        time.sleep(3)  # wait number of seconds to retry
        results = get_results()
        retries += 1

    print(results)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...