首页 \ 问答 \ 如何在Scrapy / Twisted中使用线程,即如何在响应回调中进行阻止代码的异步调用?(How to use threading in Scrapy/Twisted, i.e. how to do async calls to blocking code in response callbacks?)

如何在Scrapy / Twisted中使用线程,即如何在响应回调中进行阻止代码的异步调用?(How to use threading in Scrapy/Twisted, i.e. how to do async calls to blocking code in response callbacks?)

我需要在Scrapy中运行一些多线程\多处理工作(因为我有一些使用阻塞调用的库),并在完成之后将请求发送回Scrapy引擎。

我需要这样的东西:

def blocking_call(self, html):
    # ....
    # do some work in blocking call
    return Request(url)

def parse(self, response):
    return self.blocking_call(response.body)

我怎么能这样做? 我想我应该使用Twisted reactor和Deferred对象。 但是Scrapy parse回调必须只返回NoneRequestBaseItem对象。


I need run some multi-thread\multiprocessing work (because I have some library which uses blocking call) in Scrapy, and after its completion put back Request to Scrapy engine.

I need something like this:

def blocking_call(self, html):
    # ....
    # do some work in blocking call
    return Request(url)

def parse(self, response):
    return self.blocking_call(response.body)

How I can do that? I think I should to use Twisted reactor and Deferred object. But Scrapy parse callback must return only None or Request or BaseItem object.


原文:https://stackoverflow.com/questions/25842469
更新时间:2024-01-03 19:01

最满意答案

我真的不知道你想要实现什么,但点是正则表达式中的一个特殊字符,匹配任何单个字符。 所以string2.replace(/\D./g,'')不会“保留逗号和点”,因为正则表达式匹配\ D加上下一个字符(无论它是什么)。 如果只想匹配一个点,则必须将其转义或放入字符类。


string2 = string2.replace(/[^\d.]/g,""); I just had to add a . after the \D figured i don't need the , any way since it use 31.31 not 31,31

相关问答

更多
  • 有很多方法。但其中一个答案是: find . -name '*.html' |xargs perl -pi -e 's/find/replace/g' there are many ways .But one of the answers would be: find . -name '*.html' |xargs perl -pi -e 's/find/replace/g'
  • 您遇到的原因之一就是$()优先于反引号符号。 $()的shell解析更加一致(因为它引入了一个新的解析上下文,据我所知)。 所以你的转义虽然正确的反向代码,对于$()代码是过多的。 尝试这个: $ : > test.bin; for character in {0..255} do char=$(printf '\\x'"%02x" $character) printf "$char" >> test.bin done; hexdump -C test.bin 00000000 00 01 ...
  • tr是替换字符的正确工具: echo '`' | tr \` \' tr is the right tool to replace characters: echo '`' | tr \` \'
  • 你需要逃避\&角色: find /home/jeegar/jig.c -type f -exec \ sed -i 's/mkvFree(/mkvFree((void**)\&/g' {} \; &字符将替换整个匹配输入的模式。 You need to escape the \& character: find /home/jeegar/jig.c -type f -exec \ sed -i 's/mkvFree(/mkvFree((void**)\&/g' {} \; T ...
  • 最好的方法是使用svn merge命令,因为它将保留text.jsp文件在trunk中的完整提交日志/历史记录 svn merge --accept theirs-full https://mybranch/text.jsp https://mytrunk/text.jsp 如果存在合并冲突,则强制您使用--accept theirs-full选项忽略它们,因为您要替换文件。 如果您不想在trunk中text.jsp文件的完整提交日志, text.jsp执行以下操作。 (注意这样,提交日志将重新启动) sv ...
  • 我真的不知道你想要实现什么,但点是正则表达式中的一个特殊字符,匹配任何单个字符。 所以string2.replace(/\D./g,'')不会“保留逗号和点”,因为正则表达式匹配\ D加上下一个字符(无论它是什么)。 如果只想匹配一个点,则必须将其转义或放入字符类。 string2 = string2.replace(/[^\d.]/g,""); I just had to add a . after the \D figured i don't need the , any way since it us ...
  • 首先,您不能轻易地在用单引号分隔的参数中插入文字单引号。 你必须用' ' '"'"'或'\''替换你' 's '\'' 。 作为猜测这个问题的提示,在这里,投诉来自shell,这意味着即使你的命令行也是格式错误的。 其次,您应该知道美元和括号是sed正则表达式中的特殊字符。 你可能不得不逃避它们。 阅读man sed了解更多详情。 第三,我不确定find input.txt是否有效。 我猜你的意思是find . -type f -name input.txt find . -type f -name inp ...
  • 您的使用模式非常普遍,您可以使用它的特定实用程序,即tr tr abc ABC < input.txt > output.txt 你在哪里使用两个字符串(这里是abc和ABC )来指示你想要的替换(这里用a代替a ,用B代替b等)。 使用sed ,在tr使用方面更为通用,搜索并替换每行中的第一个匹配项。 sed 's/src1/rep1/' < in > out 要搜索并替换每行中的每个匹配项,请将g开关添加到s命令 sed 's/src1/rep1/g' < in > out 最终要做多次搜索并替换 ...
  • awk '$3=$3==1?"M":"F"' file 例如: kent$ echo "a b 1 c c d 2 x"|awk '$3=$3==1?"M":"F"' a b M c c d F x 在此示例中,您的第3列是1 or 2 ,您只需将$3更改为右列索引。 显示输入的示例以及预期输出总是很好的。 awk '$3=$3==1?"M":"F"' file for example: kent$ echo "a b 1 c c d 2 x"|awk '$3=$3==1?"M":"F"' a b ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。