Benutzer:Dirk Huenniger/py2

Aus Wikibooks
Zur Navigation springen Zur Suche springen
from multiprocessing import Process, Queue,Event
import urllib.request
import urllib




#### the less intersting part, you need to scoll down to see the 5 interesting lines.


class Future:
  def __init__(self,done,queue,process):
    self.done=done
    self.queue=queue
    self.taken=False
    self.process=process
  def get(self):
    if self.taken:
      return self.object
    else:
      self.done.wait()
      self.object=self.queue.get()
      self.taken=True
      self.process.join()
      return self.object
    
def runner(fun,q,done,args):
    q.put(fun(*args))
    done.set()
    

def runWithFuture(fun,*arguments):
 q = Queue()
 done = Event()
 p = Process(target=runner, args=(fun,q,done,arguments))
 p.start()
 return Future(done,q,p)
  

def getURL(url):
  print ("Getting URL",url)  
  x=urllib.request.build_opener().open(url).read()
  print ("Got URL",url)  
  return x

#print (len(getURL("http://www.google.de")))
class Wrapper:
  def __init__(self,content):
    self.content=content
  def __getattr__(self,name):
    print (name)
    return self.content.get().__getattribute__(name)
  def __len__(self):
    return len(self.content.get())
  def __repr__(self):
    return repr(self.content.get())

def muliproc(fun,*arguments):
  f=runWithFuture(fun,*arguments)
  return Wrapper(f)  

   

### the interesting part

a=muliproc(getURL,"http://www.google.de")
b=muliproc(getURL,"http://www.python.org")
c=muliproc(getURL,"http://www.haskell.org")

print (len(a)+len(b)+len(c) )
print (len(str(a).split("a")))

### the output

"""
Getting URL http://www.google.de
Getting URL http://www.python.org
Getting URL http://www.haskell.org
Got URL http://www.google.de
Got URL http://www.haskell.org
Got URL http://www.python.org
49690
247
"""