python3 多个线程锁_Python跨多个线程运行多个锁

所以现在的情况是,我有多个方法,它们可能同时线程化,但都需要它们自己的锁

在运行之前不会被重新线程化。它们是通过使用一些数据处理选项初始化类来建立的:class InfrequentDataDaemon(object): pass

class FrequentDataDaemon(object): pass

def addMethod(name):

def wrapper(f):

setattr(processor, f.__name__, staticmethod(f))

return f

return wrapper

class DataProcessors(object):

lock = threading.Lock()

def __init__(self, options):

self.common_settings = options['common_settings']

self.data_processing_configurations = options['data_processing_configurations'] #Configs for each processing method

self.data_processing_types = options['data_processing_types']

self.Data_Processsing_Functions ={}

#I __init__ each processing method as a seperate function so that it can be locked

for type in options['data_processing_types']:

def bindFunction1(name):

def func1(self, data=None, lock=None):

config = self.data_processing_configurations[data['type']] #I get the right config for the datatype

with lock:

FetchDataBaseStuff(data['type'])

#I don't want this to be run more than once at a time per DataProcessing Type

# But it's fine if multiple DoSomethings run at once, as long as each DataType is different!

DoSomething(data, config)

WriteToDataBase(data['type'])

func1.__name__ = "Processing_for_{}".format(type)

self.Data_Processing_Functions[func1.__name__] = func1 #Add this function to the Dictinary object

bindFunction1(type)

#Then I add some methods to a daemon that are going to check if our Dataprocessors need to be called

def fast_process_types(data):

if not example_condition is True: return

if not data['type'] in self.data_processing_types: return #Check that we are doing something with this type of data

threading.Thread(target=self.Data_Processing_Functions["Processing_for_{}".format(data['type'])], args=(self,data, lock)).start()

def slow_process_types(data):

if not some_other_condition is True: return

if not data['type'] in self.data_processing_types: return #Check that we are doing something with this type of data

threading.Thread(target=self.Data_Processing_Functions["Processing_for_{}".format(data['type'])], args=(self,data, lock)).start()

addMethod(InfrequentDataDaemon)(slow_process_types)

addMethod(FrequentDataDaemon)(fast_process_types)

对于这堵巨大的代码墙很抱歉(我已经尽我所能地修改了代码)。同样的想法是锁定每个方法

数据处理器。数据处理功能-因此,每个方法一次只能由一个线程访问(同一方法的其余线程将排队)。如何设置锁才能达到这种效果?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值