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