Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire off function without waiting for answer (Python)

I have a stream of links coming in, and I want to check them for rss every now and then. But when I fire off my get_rss() function, it blocks and the stream halts. This is unnecessary, and I'd like to just fire-and-forget about the get_rss() function (it stores its results elsewhere.)

My code is like thus:

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

(...)

def get_rss(url):
    page = urllib2.urlopen(url)     # not async
    soup = BeautifulSoup(page)

I'm thinking that if I can fire-and-forget the first call, then I can even use urllib2 wihtout worrying about it not being async. Any help is much appreciated!

Edit: Trying out gevent, but like this nothing happens:

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

The Greenlet seem to be registered, but the function self.ff.do_url(url) doesn't seem to be run at all. What am I doing wrong?

like image 781
knutole Avatar asked Nov 30 '12 16:11

knutole


1 Answers

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"
like image 192
Radix Avatar answered Oct 22 '22 22:10

Radix