2017-05-06 7 views
0

私はPythonアプリケーションでスレッディングを使用します。私はmain.pyファイル内のコードの下に使用してスレッドに労働者()関数を実行したいと仮定しますターゲットが別の.pyファイルで機能するときに、PythonでマルチスレッドAPIを使用する

def threadcalling(): 

     t = threading.Thread(target=worker()) 
     threads.append(t) 
     t.start() 

とwork.pyファイルの私のワーカー機能は、このようなものである:

def worker(): 
    """thread worker function""" 
    print 'Worker' 
    return 

マイ質問:worker()関数が私が呼んでいるのと同じ.pyファイル内にある場合、すべてのことはOKですが、worker()関数が、私のメインスクリプトでインポートした別の.pyファイルにある場合インポートエラーが発生しました:

<type 'exceptions.ValueError'> 
+1

あなたの[**完全**](https://stackoverflow.com/help/mcve)コードを投稿してください。また、なぜ手動でスレッドを管理したいのですか? ['ThreadPool'](http://stackoverflow.com/a/3386632/35070)または[' ThreadPoolExecutor'](https://docs.python.org/dev/library/concurrent.futures.html)の何が問題ですか? #threadpoolexecutor)? – phihag

+0

一般に、別のモジュールからインポートされた関数を使用することは許容されているため、特定の実装では問題になります。いくつかのデモコードなしでは、それが何であるかは言えません。戻り値が必要な場合は、キューが適しています。ほとんどの作業を行う 'multiprocessing.pool.ThreadPool'を考えてみましょう。 – tdelaney

+0

@phihagとtdelaney、問題は、私のワーカー関数が別のファイル内のクラスのメソッドであり、staticmethodのキーワークについて言及していないことでした。今私は、作業者の機能のためのstaticmethodのkeyworkを述べた、すべてのものは大丈夫です!あなたはそれについて何か考えていますか?私はまだ非常にPythonのスレッドの専門家ではない。 – Stateless

答えて

1

私にとって、この作品:I5 @

DEDEを:〜>のi5 @

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import threading 
from cc import * 

threads=[] 
t = threading.Thread(target=worker) 
threads.append(t) 
t.start() 

DEDE bb.py猫:i5の@〜>猫cc.py

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

def worker(): 
    """thread worker function""" 
    print 'Worker' 
    return 

DEDE:あなたの労働者から値を返すため

Worker 

bb.py〜>のpython()、GLを使用obal変数(ロック付き)またはキュー。

参照:https://docs.python.org/2/library/threading.html

と:https://docs.python.org/2/library/queue.html#module-Queue

関連する問題