python - pycurl subprocess in a separate Thread -
i need output pycurl, i'm trying run in subprocess. output i'm trying put in queue , pull queue out in different class.
unfortunately, right have no output =(
import threading import random import time import queue import urllib2 import sys import simplejson, pycurl import sys, signal queue = queue.queue() keep_running = true user = "username" pswd = "pass" class mythread(threading.thread): def __init__(self, queue): threading.thread.__init__(self) self.queue = queue def run(self): curl_path = '/usr/bin/curl' curl_list = [curl_path] args = ('curl', 'https://stream.twitter.com/1/statuses/filter.json?track=java', '-u', 'user:pass') arg in args: curl_list.append(arg) child = subprocess.popen( curl_list, shell=false, #stdout=subprocess.pipe) stderr=subprocess.pipe) try: out += child.communicate() c_out.write(out) self.queue.put(c_out) self.queue.task_done() except keyboardinterrupt: child.kill() class starter(): def __init__(self): self.queue = queue t = mythread(self.queue) t.daemon=true t.start() self.next() def next(self): while true: time.sleep(0.5) if not self.queue.empty(): line = self.queue.get(timeout=0.2) print '\n\nim in starter %s' % line else: print 'waiting queue' def main(): try: starter() except keyboardinterrupt, e: print 'stopping' raise main()
you seem confusing arguments subprocess quite bit... args list should of different pieces of command using curl, putting them in fashion not going work subprocess. curl_list should more this...
curl_path = '/usr/bin/curl' curl_list = [curl_path, 'https://stream.twitter.com/1/statuses/filter.json?track=java', '-u', 'user:pass']
you using unnecessary @ moment... don't want loop on list want pass subprocess handle appropriately. , going want stdout results that, need include pipe there well.
i.e, entire thing should be...
def run(self): curl_path = '/usr/bin/curl' curl_list = [curl_path, 'https://stream.twitter.com/1/statuses/filter.json?track=java', '-u', 'user:pass'] child = subprocess.popen(curl_list, shell=false, stdout=subprocess.pipe, stderr=subprocess.pipe) try: out += child.communicate()[0] c_out.write(out) self.queue.put(c_out) self.queue.task_done() except keyboardinterrupt: child.kill()
might want take @ subprocess documentation better understand changes above. haven't run through interpreter may not perfect should going in right direction... luck!