2014-10-05 4 views
0

WebView의 Skulpt를 사용하여 Python 스크립트를 실행하려고합니다. 파이썬 스크립트 무한 루프가 포함되어 있으면 응용 프로그램이 응답을 제공합니다. 자바 스크립트에서 C#잠시 후 AsyncOperation을 취소하십시오.

await webView.InvokeScriptAsync("evalPy", new string[1] { script }); 

에서 파이썬 스크립트를 실행

는 :

function evalPy(script) { 
    try { 
     var result = Sk.importMainWithBody("<stdin>", false, script); 
     return Sk.builtins.repr(result).v; 
    } catch (err) { 

    } 
} 

InvokeScriptAsyncasync 작업입니다 언제든지 취소 할 수있는 방법이있을 수 있습니다.

잠시 후 자바 스크립트를 중지하는 내 첫 번째 시도를 :

var task = webView.InvokeScriptAsync("evalPy", new string[1] { script }).AsTask<string>(); 
task.Wait(2000); 
task.AsAsyncOperation<string>().Cancel(); 

두 번째 시도 :

var op = webView.InvokeScriptAsync("evalPy", new string[1] { script }); 
new Task(async() => 
{ 
    await Task.Delay(2000); 
    op.Cancel(); 
    op.Close(); 
}).Start(); 

는 또한 자바 스크립트에

function evalPy(script) { 
    try { 
     var result = Sk.importMainWithBody("<stdin>", false, script); 
     setTimeout(function() { throw "Times-out"; }, 2000); 

     return Sk.builtins.repr(result).v; 
    } catch (err) { 
    } 
} 

CodeSkulptor.org도 Skulpt를 사용하는을의 setTimeout 시도 웹 브라우저에서 Python 스크립트를 실행하고 P의 실행을 중지합니다. 몇 시간 후 ython 스크립트.

+0

당신은'await', 방법은 늘 실행 때까지 자바 스크립트로 돌아 가면 완료되었습니다. 따라서 무한 루프가 결코 반환되지 않습니다. 2 초를 기다렸다가 취소하면 어떻게됩니까? –

+0

2 초 동안 기다렸다가 취소하면 무한 루프가 계속됩니다. 디버거가 작업 상태를 취소로 표시하지만 응용 프로그램이 응답하지 않음 –

+0

파이썬 프로세스가 계속 실행되지만 메서드 호출이 반환됩니다. 메소드 호출에서 다른 일을하고 있습니까? –

답변

1

저는 Codecademy, html 과정을 크롤링했는데 세부 사항을 알지 못했지만 자바 스크립트는 단일 스레드 언어이며 여러 스레드에 웹 작업자가 필요하다고 들었습니다.

importScripts('./skulpt.js'); 
importScripts('./skulpt.min.js'); 
importScripts('./skulpt-stdlib.js'); 

// file level scope code gets executed when loaded 


// Executed when the function postMessage on 
//the worker object is called. 
// onmessage must be global 
onmessage = function(e){ 
    var out = []; 
    try{ 
    Sk.configure({output:function (t){out.push(t);}}); 
    Sk.importMainWithBody("<stdin>",false,e.data); 
    }catch(e){out.push(e.toString());} 
    postMessage(out.join('')); 
} 

메인 페이지 스크립트 (테스트하지) : 단점 내가 파이썬 코드에서() 입력을 사용할 때, skulpt이 창 개체를 찾을 수 없다는 오류가 발생합니다,하지만이

var skulptWorker = new Worker('SkulptWorker.js'); 
skulptWorker.onmessage = function(e){ 
    //Writing skulpt output to console 
    console.log(e.data); 
    running = false; 
} 
var running = true; 
skulptWorker.postMessage('print(\'hello world\')'); 
running = true; 
skulptWorker.postMessage('while True:\n print(\'hello world\')'); 


setTimeout(function(){ 
    if(running) skulptWorker.terminate();},5000); 

그것은 작업 스레드에 있고 아직 그것에 대한 해결책이 없습니다.

p.s.

SkulptWorker.js : 일부 테스트는 코드가 아래 (스팸를 PostMessage 나쁜 생각입니다) 메인 쓰레드를 정지 밝혀

importScripts('./skulpt.js'); 
importScripts('./skulpt.min.js'); 
importScripts('./skulpt-stdlib.js'); 

// file level scope code gets executed when loaded 


// Executed when the function postMessage on 
//the worker object is called. 
// onmessage must be global 
onmessage = function(e){ 
    try{ 
    Sk.configure({output:function (t){postMessage(t);}}); 
    Sk.importMainWithBody("<stdin>",false,e.data); 
    }catch(e){postMessage(e.toString());} 
}