English 中文(简体)
晚间复读
原标题:Recursive thread creation in python

I m试图执行一个补习的Fibonacci系列,该系列的数值按指数计算。 它是一个家庭工作,需要使用多读方法。 这是我迄今所做的工作。 我的问题是,我如何增加<条码>、有效期_thread1和<条码>_live_thread2的结果。 必须在各个层次上为再入侵创造read。

def Recursive(n):
    if n< 2:
        return n
    else:
        return Recursive(n- 1) + Recursive(n- 2)



def FibonacciThreads(n):
    if n< 2:
        return n
    else:
        thread1        = threading.Thread(target=FibonacciThreads,args=(n-1,))
        thread2        = threading.Thread(target=FibonacciThreads,args=(n-2,))
        thread1.start()
        thread2.start()
        thread1.join()
        thread2.join()
        return live_thread1+live_thread2
最佳回答

你们可以把 object子带上read子,用于储存结果。 如果你不想引入新的数据类型,那么,例如,你可以仅使用单一要素清单:

def fib(n, r):
    if n < 2:
        r[0] = n
    else:
        r1 = [None]
        r2 = [None]
        # Start fib() threads that use r1 and r2 for results.
        ...

        # Sum the results of the threads.
        r[0] = r1[0] + r2[0]

def FibonacciThreads(n):
    r = [None]
    fib(n, r)
    return r[0]
问题回答

这样做是不可能的,因为你无法收回在另一条路面执行的职能的回报价值。

为了实施你想要的行为,你必须制定<代码>。 FibonacciThreads , a callsable Object that provide the result as a member changing:

class FibonacciThreads(object):
    def __init__(self):
        self.result = None

    def __call__(self, n):
        # implement logic here as above 
        # instead of a return, store the result in self.result

你们可以利用这种职能:

fib = FibonacciThreads() # create instance
fib(23) # calculate the number
print fib.result # retrieve the result

请注意,正如我在评论中所说的那样,这并不是一个非常明智地利用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 ]="...

热门标签