8

我有一个链接流进来,我想不时检查它们rss。但是当我启动我的get_rss()功能时,它会阻塞并且流停止。这是不必要的,我只想一劳永逸地忘记该get_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

Greenlet 似乎已注册,但该功能self.ff.do_url(url)似乎根本没有运行。我究竟做错了什么?

4

3 回答 3

8

Fire and forget using the multiprocessing module:

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"
于 2016-06-28T19:29:38.513 回答
1
  1. here is sample code for thread based method invocation additionally desired threading.stack_size can be added to boost the performance.
import threading
import requests

#The stack size set by threading.stack_size is the amount of memory to allocate for the call stack in threads.
threading.stack_size(524288)

def alpha_gun(url, json, headers):
    #r=requests.post(url, data=json, headers=headers)
    r=requests.get(url)
    print(r.text)


def trigger(url, json, headers):
    threading.Thread(target=alpha_gun, args=(url, json, headers)).start()


url = "https://raw.githubusercontent.com/jyotiprakash-work/Live_Video_steaming/master/README.md"
payload="{}"
headers = {
  'Content-Type': 'application/json'
}

for _ in range(10):
    print(i)
    #for condition 
    if i==5:
        trigger(url=url, json =payload, headers=headers)
        print('invoked')
    
于 2022-01-08T14:59:02.263 回答
0

You want to use the threading module or the multiprocessing module and save the result either in database, a file or a queue.

You also can use gevent.

于 2012-11-30T16:27:26.500 回答