如何在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
回调必须只返回None
或Request
或BaseItem
对象。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 onlyNone
orRequest
orBaseItem
object.
原文:https://stackoverflow.com/questions/25842469
最满意答案
我真的不知道你想要实现什么,但点是正则表达式中的一个特殊字符,匹配任何单个字符。 所以
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 ...
-
!:gs/jpg/png !:gs/jpg/png
-
最好的方法是使用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 ...
-
如何:替换命令(How to: replace command)[2022-01-02]
我真的不知道你想要实现什么,但点是正则表达式中的一个特殊字符,匹配任何单个字符。 所以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 ... -
替换命令有问题(Problem with replace command)[2023-04-07]
首先,您不能轻易地在用单引号分隔的参数中插入文字单引号。 你必须用' ' '"'"'或'\''替换你' '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 ...