English 中文(简体)
动态装饰模块(+多处理问题)
原标题:Dynamically loading modules in Python (+ multi processing question)
  • 时间:2010-05-31 07:33:09
  •  标签:
  • python

我正在撰写一份“灰色”包,其中从一个组合档案中读到单元清单(加上附属数据)。

然后,我想通过每个有活力的模块加以调整,并援引一种“工作”(工作)”职能,从而启动一个新进程,使《海关法》在一个单独的进程中运作。

目前,我进口了我主稿开始时所有已知单元的清单——这是我感到的新生的黑体,不十分灵活,也是一种维持痛苦。

这一职能推动了这些进程。 我将加以修改,以便在遇到这一问题时积极装上模块。 字典的关键是包含该代码的模块的名称:

def do_work(work_info):
  for (worker, dataset) in work_info.items():
    #import the module defined by variable worker here...

    # [Edit] NOT using threads anymore, want to spawn processes asynchronously here...

    #t = threading.Thread(target=worker.do_work, args=[dataset])
    # I ll NOT dameonize since spawned children need to clean up on shutdown
    # Since the threads will be holding resources
    #t.daemon = True
    #t.start()

<><><><><><>><>>><>>>>>>

当我用我的文字(如上文)称职时,我有以下错误:

AttributeError: str object has no attribute do_work

这样做是有意义的,因为词典钥匙是一种扼杀(即将进口的单元名称)。

在我补充发言时:

<> 进口工人

在穿透镜前,我发现错误:

进口错误: 没有一个称为工人的单元

这是奇怪的,因为现在使用的是变名称,而不是它所持有的价值——在我印刷变数时,我得到的是(我预期的)什么?

<><> 问 题 2

正如我在评论部分提到的那样,我认识到,在被间谍儿童中书写的工作职能本身需要清理。 我的理解是,写上“清理”职能,当这项工作成功完成时,或发现一个无操作的例外情况时,我是否还需要做任何事情来确保资源不泄露,或让本组织在一个不稳定的国家离开?

< 问题3

如果我评论一下T.daemon的旗帜,那么该法典是否掌握了ASYNCHRONOUSLY? 间谍儿童从事的工作相当密集,我不想等待一名儿童在照看另一名儿童之前就结束。 BTW,我知道,在甲型六氯环己烷中的渗透实际上是现实的,是一种时间分享/利用——即ok。

最后,有更好的(更富沙里)方式来做我想要做的事情吗?

<><>>

在阅读了有关沙夫·GIL和在沙尔的翻新(ahem - hack)的略多一点之后,我认为最好使用不同的程序(至少是国际独立调查委员会,如果能够利用多种程序的话,则该文字可以加以利用),因此,我会发现新的进程而不是read。

我对产卵过程有某些样本代码,但这是一种比喻的三维(利用lam贝功能)。 我愿知道如何扩大这一结构,以便它能够处理负荷模块中的运行职能(如我前面所做的那样)。

这是我所珍视的:

def do_mp_bench():
    q = mp.Queue() # Not only thread safe, but "process safe"
    p1 = mp.Process(target=lambda: q.put(sum(range(10000000))))
    p2 = mp.Process(target=lambda: q.put(sum(range(10000000)))) 
    p1.start()
    p2.start()
    r1 = q.get()
    r2 = q.get()
    return r1 + r2

我如何加以修改,以便处理单元的字典,并在新过程中操作每个装满模块的工作。

最佳回答
问题回答

问题1:使用import__(

问题2:为什么在<代码>上完成清理工作(>功能)不在此限?

问题3:国际红十字与红新月联会的透镜仅仅意味着该方案自动地等待这一read子结束。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...