2017-08-15 4 views
0

Ich versuche, mit Asyncio in den Griff zu bekommen, um den Aufruf eines externen Tools zur Analyse mehrerer Audiodateien zu beschleunigen. Ich arbeite an Windows, Python 3.6 (Anaconda Installation) und das Problem, das ich hier habe, ist, dass die Anrufe nicht zu warten scheinen oder die Ergebnisse nie durch stdout erhalten werden.Asyncio stdout - fehlgeschlagen

Irgendwelche Ideen?

import os 
import asyncio 

external_tool = r"C:\path\to\external\tool.exe" 

def filepaths_generator(root): 
    ''' 
    Simple generator to yield filepaths 
    ''' 
    for path, dirs, files in os.walk(root): 
     for f in files: 
      yield os.path.join(path,f) 


async def async_subprocess_command(*args): 
    ''' 
    A function to run the asyncio subprocess call 
    ''' 
    # Create subprocess 
    process = asyncio.create_subprocess_exec(
     *args, 
     # stdout must a pipe to be accessible as process.stdout 
     stdout=asyncio.subprocess.PIPE) 
    # Wait for the subprocess to finish 
    stdout, stderr = await process.communicate() 
    # Return stdout 
    return stdout.decode().strip() 

async def add_external_tool_data_to_dict(filepath): 
    external_tool_data = await async_subprocess_command(external_tool,filepath) 
    metadata_dict[filepath] = external_tool_data 
    return 

metadata_dict = {} 

loop = asyncio.get_event_loop() 
#loop = asyncio.ProactorEventLoop() - Tried this, it doesn't help! 

filepaths = filepaths_generator(r"C:\root\path") 
tasks = [] 

for filepath in filepaths: 
    #Create tasks in for the loop and add to a list 
    task = loop.create_task(add_external_tool_data_to_dict(filepath)) 
    tasks.append(task) 
#Run the tasks 
loop.run_until_complete(asyncio.wait(tasks)) 

loop.close() 
print(metadata_dict) 
+0

Wenn ich die Schleife verwenden = asyncio.ProactorEventLoop() bekomme ich den Fehler: – user3535074

+0

Datei „C: \ Users \ danie \ Anaconda3 \ lib \ asyncio \ base_events.py“, Linie 340, in _make_subprocess_transport Erhöhung NotImplementedError NotImplementedError – user3535074

+0

Nachdem Sie die' Schleife versucht = asyncio.ProactorEventLoop() ', haben Follow-up mit:' asyncio.set_event_loop (Schleife) '? – Gerrat

Antwort

2

Für den Anfang gibt es this:

On Windows, the default event loop is SelectorEventLoop which does not support subprocesses. ProactorEventLoop should be used instead

Beispiel es unter Windows zu verwenden:

import asyncio, sys 

if sys.platform == 'win32': 
    loop = asyncio.ProactorEventLoop() 
    asyncio.set_event_loop(loop) 

ich durch den Wechsel zu dieser alternativen Ereignisschleife beginnen würde.

0

Per Gerrat Kommentar war die Lösung die ProactorEventLoop und fügen eine zusätzliche Zeile danach zu verwenden:

loop = asyncio.ProactorEventLoop() 
asyncio.set_event_loop(loop)