twisted笔记三:當Deferred遇上Thread(转载)

       Deferred不會自動實現將阻塞過程轉為非阻塞過程,雖然它已經有那樣的機制但還是要你去多走一步。要將阻塞過程轉為真正的非阻塞過程,那麼只有借用線程。但至於線程調用你不用太擔心,twisted已為你準備好一個方便的用法。就是將Deferred綁在Thread上,這樣就有了deferToThread,味道不比牛奶加巧克力差。
        deferToThread在twisted文檔的說明,洋墨水喝得比較少,就不照字面翻譯了。大概意思是將函數f放在線程裡並作為Deferred返回,args和kwargs是函數f的參數。也就是使用deferToThread返回的是一個帶線程的Deferred,並自動使用callback調用指定的函數f。相當於在線程中運行下面的代碼
    d=defer.Deferred().addCallback(f)
    d.callback(result)
 

def deferToThread(f, *args, **kwargs): (source)

Run a function in a thread and return the result as a Deferred.

ParametersfThe function to call.
*argspositional arguments to pass to f.
**kwargskeyword arguments to pass to f.
Returns

A Deferred which fires a callback with the result of f, or an errback with a twisted.python.failure.Failure if f throws an exception.

#!/usr/bin/env python
#coding=utf-8

from twisted.internet import protocol,reactor,defer
from twisted.internet.threads import deferToThread
from twisted.protocols import basic
import time,sys
syscode='big5'

class muProtocol(basic.LineReceiver):

    def connectionMade(self):
        print 'connectionMade'
        self.factory.clients[self]={}
        self.factory.clients[self]['isblankly']=True

    def lineReceived(self,data):
        self.factory.clients[self]['isblankly']=False
        self.transport.write(self.factory.getSomething(data))
        if data=='q':
            self.transport.loseConnection()
        self.factory.kungfuTea(data)

class muFactory(protocol.ServerFactory):
    protocol=muProtocol

    def __init__(self):
        reactor.callLater(1,self.timeout)
        self.clients={}

    def timeout(self):
        sys.stdout.write('.')
        sys.stdout.flush()
        reactor.callLater(1,self.timeout)

    def getSomething(self,data):
        return '\n\rServer say: %s\n\r' % data

    def goodTaste(self,data):
        print u'%s:耍下太極,仲加埋嘆下功夫茶,都有排等羅。'.encode(
            syscode,'replace') % data
        time.sleep(10)
        print u'%s,打完收功。'.encode(syscode,'replace') % data
        return data

    def wrongTaste(self,data):
        print u'%s:好味道盡在kung fu tea。'.encode(syscode,'replace') % data

    def kungfuTea(self,data):
        return deferToThread(self.goodTaste,data
            ).addBoth(self.wrongTaste)

reactor.listenTCP(20190,muFactory())
reactor.run()

     分析下上面的代碼。
    當接收到數據後調用self.factory.kungfuTea(data)
    def lineReceived(self,data):
        self.factory.kungfuTea(data)
    工廠中的kungfuTea只是使用deferToThread將真正處理數據的過程轉goodTaste為一個線程的Deferred,剩下的東西就和Deferred操作無異。

    牛奶加巧克力不錯,老板再來一杯!

传送门:
[twisted笔记一:Deferred笔记(搬运)](https://blog.csdn.net/huangzyi/article/details/112559998)
[twisted笔记二:twisted的那三板斧(转载)](https://blog.csdn.net/huangzyi/article/details/112561779)
[twisted笔记三:當Deferred遇上Thread(转载)](https://blog.csdn.net/huangzyi/article/details/112563308)

转自:https://sites.google.com/site/mypynotes/twisted-1/defertothread

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值