Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不等待回答的情况下启动函数(Python)_Python_Asynchronous_Tornado_Pycurl - Fatal编程技术网

在不等待回答的情况下启动函数(Python)

在不等待回答的情况下启动函数(Python),python,asynchronous,tornado,pycurl,Python,Asynchronous,Tornado,Pycurl,我有一个链接流进来,我想时不时地检查它们的rss。但当我启动get\u rss()函数时,它会阻塞,流停止。这是不必要的,我只想启动并忘记get\u rss()函数(它将结果存储在其他地方) 我的代码是这样的: self.ff.get_rss(url) # not async print 'im back!' (...) def get_rss(url): page = urllib2.urlopen(url) # not async soup = Beauti

我有一个链接流进来,我想时不时地检查它们的
rss
。但当我启动
get\u rss()
函数时,它会阻塞,流停止。这是不必要的,我只想启动并忘记
get\u rss()
函数(它将结果存储在其他地方)

我的代码是这样的:

self.ff.get_rss(url)    # not async
print 'im back!'

(...)

def get_rss(url):
    page = urllib2.urlopen(url)     # not async
    soup = BeautifulSoup(page)
我在想,如果我可以启动并忘记第一个调用,那么我甚至可以使用urllib2,而不用担心它不是异步的。非常感谢您的帮助

编辑: 尝试gevent,但像这样什么都不会发生:

print 'go'
g = Greenlet.spawn(self.ff.do_url, url)
print g
print 'back'

# output: 
go
<Greenlet at 0x7f760c0750f0: <bound method FeedFinder.do_url of <rss.FeedFinder object at 0x2415450>>(u'http://nyti.ms/SuVBCl')>
back
打印“开始”
g=Greenlet.spawn(self.ff.do_url,url)
打印g
打印“返回”
#输出:
去
返回
Greenlet似乎已注册,但函数
self.ff.do\u url(url)
似乎根本没有运行。我做错了什么?

您想使用模块或模块并将结果保存在文件或文件夹中


您还可以使用。

使用多处理模块触发并忘记:

def fire_and_forget(arg_one):
    # do stuff
    ...

def main_function():
    p = Process(target=fire_and_forget, args=(arg_one,))
    # you have to set daemon true to not have to wait for the process to join
    p.daemon = True
    p.start()
    return "doing stuff in the background"

线程是你的朋友,但是每秒钟启动一个新线程不是有点多吗?不,不是,但你不需要,你可以启动3个线程并通过一个公共队列向它们提交URL。我如何用gevent编写它?非常感谢你的回答。但我被上面的gevent困住了。有什么想法吗?谢谢