python class 내부 함수(self)를 사용하는 pool 질문
조회수 719회
MyApp이라는 class 내부의
def work_func(self, x):
print("value %s is in PID : %s" % (self.a[x], os.getpid()))
time.sleep(1)
return self.a[x] ** 5
work_func 함수를 멀티쓰래드로 실행시키려고 합니다.
실행 후, [pool2] 버튼을 클릭하면 pool이 만들어지고 work_func을 호출하게 되는데
def pool2(self):
start = int(time.time())
num_cores = 6
self.a = list(range(0, 12))
a = list(range(0, 12))
print(self.a)
with Pool(num_cores) as pool:
print(pool.map(self.work_func, a))
print("***run time(sec) :", int(time.time()) - start)
아래 코드를 수행할 때 프로그램이 죽습니다.
print(pool.map(self.work_func, a))
work_func 의 인자 때문인 것 같습니다
[질문]
어떻게 해야 pool2 함수가 제대로 수행이 될 수 있을까요?
*class 내부 함수를 mapping 하는 방법을 찾고있습니다.
코드 전문
import time, os
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
from multiprocessing import Pool
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn4 = QPushButton('pool2', self)
btn4.clicked.connect(self.pool2)
vbox = QVBoxLayout()
vbox.addWidget(btn4)
self.a = []
self.setLayout(vbox)
self.setWindowTitle('QPushButton')
self.setGeometry(300, 300, 300, 200)
self.show()
def work_func(self, x):
print("value %s is in PID : %s" % (self.a[x], os.getpid()))
time.sleep(1)
return self.a[x] ** 5
def pool2(self):
start = int(time.time())
num_cores = 6
self.a = list(range(0, 12))
a = list(range(0, 12))
print(self.a)
with Pool(num_cores) as pool:
print(pool.map(self.work_func, a))
print("***run time(sec) :", int(time.time()) - start)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력