2016-08-16 6 views
0

C#コードをpythonに複製しようとしていますが、スレッドを実行して終了するのを待って値を返します。基本的には、メソッドRunAndWaitは、そのメソッドの呼び出しが複数回行われているため、ヘルパークラスに含まれています。次のように別のモジュールの別のスレッドから戻り値を受け取る

C#コードは次のとおりです。

python
public static bool RunAndWait(Action _action, long _timeout) 
    { 
     Task t = Task.Run(() => 
     { 
      Log.Message(Severity.MESSAGE, "Executing " + _action.Method.Name); 
      _action(); 
     }); 
     if (!t.Wait(Convert.ToInt32(_timeout))) 
     { 
      Log.Message(Severity.ERROR, "Executing " + _action.Method.Name + " timedout. Could not execute MCS command."); 
      throw new AssertFailedException(); 
     } 
     t.Dispose(); 
     t = null; 
     return true; 
    } 

私はいくつかのことで苦労されています。まず、異なるタイプのキューがあるように思えます。ここでは、単に仕事をしていると思われるインポートを選択しました。import Queue。次に、私は以下のようにTypeErrorを受け取ります。

from Libs.CreateConnection import CreateMcsConnection 
import Libs.MonkeyHelper as mh 
import Queue 

q = Queue.Queue() 
to = 5000 #timeout 

mh.RunCmdAndWait(CreateMcsConnection, to, q) 
serv, con = q.get() 

MonkeyHelper.py:ここ

Traceback (most recent call last): File "C:/Users/JSC/Documents/Git/EnterprisePlatform/Enterprise/AI.App.Tool.AutomatedMachineTest/Scripts/monkey.py", line 9, in File "C:\Users\JSC\Documents\Git\EnterprisePlatform\Enterprise\AI.App.Tool.AutomatedMachineTest\Scripts\Libs\MonkeyHelper.py", line 4, in RunCmdAndWait TypeError: module is not callable

は猿のためpythonコードです

import threading 

def RunCmdAndWait(CmdToRun, timeout, q): 
    t = threading(group=None, target=CmdToRun, arg=q) 
    t.start() 
    t.join(timeout=timeout) 

私は私が間違っているのかわからないです。私はかなり新しいPythonです。誰かが私を助けてくれますか?別のエラー育て上記の行を修正

編集

t = threading.Thread(group=None, target=CmdToRun, args=q) 

Exception in thread Thread-1: Traceback (most recent call last): File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 552, in _Thread__bootstrap_inner self.run() File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 505, in run self.target(*self.__args, **self.__kwargs) AttributeError: Queue instance has no attribute '__len'

Threadが複数の引数を期待しているためか、queueはまだこの時点では空であるためということですか?私が見たところでは、queueが戻り値を受け取るための引数として渡されているだけです。それは正しい方法ですか?以下はTypeErrorで

EDIT2

変更t = threading.Thread(group=None, target=CmdToRun, args=q)

t = threading.Thread(group=None, target=CmdToRun, args=(q,))に変更利回り、スレッドはタプルを期待されているので、私には奇妙なようです。

Exception in thread Thread-1: Traceback (most recent call last): File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 552, in _Thread__bootstrap_inner self.run() File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 505, in run self.__target(*self.__args, **self.__kwargs) TypeError: tuple is not callable

+0

'threading.Thread()'が必要です。エラーを注意深く見れば、それは理にかなっています。 – sberry

答えて

1

threadingはモジュールです。あなたはおそらく

t = threading.Thread(group=None, target=CmdToRun, args=(q,)) 

argsは、引数のタプルであると

t = threading(group=None, target=CmdToRun, arg=q) 

を交換することを意味します。

+0

ありがとうございました。問題が修正されました。オブジェクトが 'Thread'の間、モジュールは' threading'です。これは新しいエラーを引き起こす。編集を参照してください。 –

+0

もう一度お手伝いしてくれてありがとう。私は、 'TypeError:tuple is callable'という結果になる' args =(q、) 'を渡して第2の変更を適用しました。 –

関連する問題