python装饰器类型错误_python – 在装饰器内部使用多处理会产生错误:无法解析函数…它找不到...

我遇到了一个我无法解决的问题,它与多处理相关并在装饰器中使用它.

当我使用多处理调用方法run_in_parallels时,我收到错误:

无法在0x00000000027789C8>处发现< function run_testcase:找不到__main __.run_testcase 调用发生在装饰器内部,然后是上述问题.在调用相同的方法时,run_in_parallels没有装饰器都正常工作. 这个问题的原因是什么? file:w_PythonHelper.py

desc:函数’run_in_parallel’用于同时运行多个进程.第一种方法,即结束操作将停止其他操作.

from multiprocessing import Process,Event

class ExtProcess(Process):

def __init__(self, event,*args,**kwargs):

self.event=event

Process.__init__(self,*args,**kwargs)

def run(self):

Process.run(self)

self.event.set()

class PythonHelper(object):

@staticmethod

def run_in_parallel(*functions):

event=Event()

processes=dict()

for function in functions:

fname=function[0]

try:fargs=function[1]

except:fargs=list()

try:fproc=function[2]

except:fproc=1

for i in range(fproc):

process=ExtProcess(event,target=fname,args=fargs)

process.start()

processes[process.pid]=process

event.wait()

for process in processes.values():

process.terminate()

for process in processes.values():

process.join()

file:w_Recorder.py

desc:函数’capture’用于获取截图

from PIL import ImageGrab

import time

class Recorder(object):

def capture(self):

ImageGrab.grab().save("{f}.{e}".format(f=time.time(),e="png"))

file:w_Decorators.py

desc:并行运行给定函数以及类’Recorder’的方法’capture’

from w_Recorder import Recorder

from w_PythonHelper import PythonHelper

def check(function):

def wrapper(*args):

try:

recorder=Recorder()

PythonHelper.run_in_parallel([function,args],[recorder.capture])

print("success")

except Exception as e:

print("failure: {}".format(e))

return function

return wrapper

file:w_Logger.py

desc:主程序(生成错误)

from w_Decorators import check

import time

class Logger(object):

@check

def run_testcase(self):

# example function (runtime: 20s)

for i in range(20):

print("number: {}".format(i))

time.sleep(1)

def run_logger(self):

self.run_testcase()

if __name__=="__main__":

logger=Logger()

logger.run_logger()

file:w_Logger.py

desc:主程序(核心工作)

from w_PythonHelper import PythonHelper

from w_Recorder import Recorder

import time

class Logger(object):

def run_testcase(self):

# example function (runtime: 20s)

for i in range(20):

print("number: {}".format(i))

time.sleep(1)

def run_logger(self):

recorder=Recorder()

PythonHelper.run_in_parallel([self.run_testcase],[recorder.capture])

if __name__=="__main__":

logger=Logger()

logger.run_logger()

在这两种情况下,这些相同的方法有何不同?

编辑:

有没有人有想法解决这个问题(这是Python的错误)?如果没有,也许有人知道在应用程序运行时捕获屏幕截图的好方法?

答案是:要解决此问题,您可以删除进程成员.但是我如何为我的示例执行此操作.

在run_in_parallel(* functions)中调用process.start()时发生调试错误

EDIT2:

像ivan_pozdeev写的:我可以使用包装器作为函数,但不能将它用作装饰器.我有很多由这个装饰器装饰的函数,最简单的方法是在装饰器里面使用多处理.但不幸的是我无法解决这个问题.也许有人已经解决了类似的问题.任何暗示我都会感激不尽.

‘run_in_parallel’功能就像我想要的那样.两个或多个函数并行运行,第一个函数完成,强制终止第二个函数.当我调用包装器(函数,* args)然后函数工作正常,当我把这个机制放在装饰器里面时我得到’不能发痒的功能……它没有被发现’错误.细节可以在上面找到

我的追溯:

Traceback (most recent call last):

File "C:\Interpreters\Python32\lib\pickle.py", line 679, in save_global

klass = getattr(mod, name)

AttributeError: 'module' object has no attribute 'run_testcase'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "C:\EskyTests\w_Logger.py", line 19, in

logger.run_logger()

File "C:\EskyTests\w_Logger.py", line 14, in run_logger

self.run_testcase()

File "C:\EskyTests\w_Decorators.py", line 14, in wrapper

PythonHelper.run_in_parallel([function,args],[recorder.capture])

File "C:\EskyTests\w_PythonHelper.py", line 25, in run_in_parallel

process.start()

File "C:\Interpreters\Python32\lib\multiprocessing\process.py", line 130, in start

self._popen = Popen(self)

File "C:\Interpreters\Python32\lib\multiprocessing\forking.py", line 267, in __init__

dump(process_obj, to_child, HIGHEST_PROTOCOL)

File "C:\Interpreters\Python32\lib\multiprocessing\forking.py", line 190, in dump

ForkingPickler(file, protocol).dump(obj)

File "C:\Interpreters\Python32\lib\pickle.py", line 237, in dump

self.save(obj)

File "C:\Interpreters\Python32\lib\pickle.py", line 344, in save

self.save_reduce(obj=obj, *rv)

File "C:\Interpreters\Python32\lib\pickle.py", line 432, in save_reduce

save(state)

File "C:\Interpreters\Python32\lib\pickle.py", line 299, in save

f(self, obj) # Call unbound method with explicit self

File "C:\Interpreters\Python32\lib\pickle.py", line 623, in save_dict

self._batch_setitems(obj.items())

File "C:\Interpreters\Python32\lib\pickle.py", line 656, in _batch_setitems

save(v)

File "C:\Interpreters\Python32\lib\pickle.py", line 299, in save

f(self, obj) # Call unbound method with explicit self

File "C:\Interpreters\Python32\lib\pickle.py", line 683, in save_global

(obj, module, name))

_pickle.PicklingError: Can't pickle : it's not found as __main__.run_testcase

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值