【发布时间】:2019-10-22 14:39:26
【问题描述】:
该程序分为三个部分:将数据添加到队列中,处理队列中的数据和检查队列是否为空。每个除了 Add() 之外,所有部分都有自己的进程。程序应该是这样运行的,当我们启动它时,
我正在使用 Windows,并且一直在使用
TypeError: can't pickle _thread.lock objects
这里是代码
from multiprocessing import Queue,Process
from time import sleep
import threading
from multiprocessing.pool import ThreadPool
from multiprocessing import dummy as multithreading
import concurrent.futures
# import queue
class A(object):
def __init__(self, *args, **kwargs):
self.Q = Queue()
#Add data to queue: should be accessable all time
def Add(self, i):
# q = Queue()
self.Q.put(threading.Thread(target=self.printAns(i)))
#Processes the data: runs only upon call
def printAns(self,name):
print("Name to print is: ",name)
return 'completed'
#This function call printANS as a process
def jobRun(self):
# job = self.Q.get()
# ans = Queue()
jobThread = self.Q.get()
async_result = jRPool.apply_async(jobThread)
print(async_result.get())
#Checks if the queue has anything: checker functions needs to run constantly
def checkQueue(self):
while True:
if self.Q.empty():
pass
else:
return True
#should initiate call to checker upon success calls jobRun() as a process and go back to checking
def run(self):
with concurrent.futures.ProcessPoolExecutor() as executor:
checkfunc = executor.map(self.checkQueue)
while True:
if checkfunc:
sleep(1)
executor.map(self.jobRun)
self.Q.close()
if __name__ == '__main__':
a = A()
a.Add("test1")
a.Add("test2")
a.run()
# a.Add("this")
while True:
data = input("Enter a string: ")
a.Add(data)
我们深表感谢任何形式的帮助。我的预感与锁或信号量有关。
【问题讨论】:
-
为什么不简单地使用
multiprocessing.Pool和apply_async来提交作业?
标签: python python-3.x asynchronous parallel-processing threadpool