4

我有一个小程序,当我通过zerorpcpython 2.7 中的模块调用它时,它会在后台进行一些计算。

这是我的代码:

is_busy = False

class Server(object):
   def calculateSomeStuff(self):
       global is_busy

        if (is_busy):
            return 'I am busy!'

        is_busy = True

        # calculate some stuff

        is_busy = False
        print 'Done!'
        return 

    def getIsBusy(self):
        return is_busy

s = zerorpc.Server(Server())
s.bind("tcp://0.0.0.0:66666")
s.run()

is_busy当我调用.getIsBusy()方法时,我应该改变什么来让这个程序返回,然后.calculateSomeStuff()开始做它的工作?

据我所知,没有办法让它在 python 2 中异步。

4

1 回答 1

3

You need multi-threading for real concurrency and exploit more than one CPU core if this is what you are after. See the Python threading module, GIL-lock details & possible workarounds and literature.

If you want a cooperative solution, read on.

zerorpc uses gevent for asynchronous input/output. With gevent you write coroutines (also called greenlet or userland threads) which are all running cooperatively on a single thread. The thread in which the gevent input output loop is running. The gevent ioloop takes care of resuming coroutines waiting for some I/O event.

The key here is the word cooperative. Compare that to threads running on a single CPU/core machine. Effectively there is nothing concurrent,
but the operating system will preempt ( verb: take action in order to prevent (an anticipated event) from happening ) a running thread to execute the next on and so on so that every threads gets a fair chance of moving forward.

This happens fast enough so that it feels like all threads are running at the same time.

If you write your code cooperatively with the gevent input/output loop, you can achieve the same effect by being careful of calling gevent.sleep(0) often enough to give a chance for the gevent ioloop to run other coroutines.

It's literally cooperative multithrading. I've heard it was like that in Windows 2 or something.

So, in your example, in the heavy computation part, you likely have some loop going on. Make sure to call gevent.sleep(0) a couple times per second and you will have the illusion of multi-threading.

I hope my answer wasn't too confusing.

于 2016-03-18T08:57:05.457 回答