🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.python
Article #27317

Re: jak sensownie polaczyc watki z rekurencja?

#27317
From: Łukasz Rekucki
Date: Thu, 25 Feb 2010 17:14
81 lines
3950 bytes
On Feb 25, 5:52 pm, Slawomir Piasecki <piase...@gmail.com> wrote:
> ok wiec multiprocess, poczytalem troche dokumentacji i troche
> tutoriali
> i postanowilem wypisac wstepnie maly programik:
>
> [code]
> def f(q, n):
>     for i in range(2,4):
>       t = i * n
>
>       if t < 100:
>         print "dodaje ", t
>         q.put(t) # dodaj do kolejki
>
> def run(nrStart):
>      q = Queue()
>
>      for i in range(nrStart):
>        q.put(i) # dodaj do kolejki
>
>      nr = 2
>      pula = []
>
>      while True:
>        for i in pula:
>          if i.is_alive() is False:
>            pula.remove(i) # usun z puli jesli juz wykonany
>
>        while len(pula) < nr:
>            if q.qsize() > 0:
>              dat = q.get() # pobierz z kolejki
>              print "startuje ", dat
>              p = Process(target=f, args=(q, dat)).start() # wystartuj
> z procesem
>              pula.append(p) # dodaj do puli
>
>            else:
>              if len(pula) == 0:
>                return # zakoncz
>              else:
>                break # wroc do petli
>
>        sleep(5) # czekaj
>
> if __name__ == '__main__':
>  run(5)
>
> [/code]
>
> i dostaje po odpaleniu komunikat:
>   File "tst.py", line 28, in run
>     if i.is_alive() is False:
> AttributeError: 'NoneType' object has no attribute 'is_alive'
> Exception in thread QueueFeederThread (most likely raised during
> interpreter shutdown):
> Traceback (most recent call last):
>   File "/usr/lib/python2.6/threading.py", line 522, in
> __bootstrap_inner
>   File "/usr/lib/python2.6/threading.py", line 477, in run
>   File "/usr/lib/python2.6/multiprocessing/queues.py", line 233, in
> _feed
> <type 'exceptions.TypeError'>: 'NoneType' object is not callable
>
> wiec tutaj sie blokuje i nie wiem jak sprawdzic czy dany process jesce
> dziala czy nie,
>
> pozdrawiam
> p.s.
Robisz:
p = Process(target=f, args=(q, dat)).start()

Metoda start() nie zwraca obiektu na którym została wywołana, tylko
None (bo to procedura). Powinno być:

p = Process(target=f, args=(q, dat))
p.start()

PS. "if i.is_alive() is False:" bym zamienił na  "if not
process.is_alive():" - jakoś tak bardziej czytelnie. Zastanowiłbym się
też nad faktem, że aktualnie właściwie robisz aktywne oczekiwanie. Z
opóźnieniem 5 sekund, ale zawsze.
Technical Details
Message-ID: <1caeb989-aee5-4af8-81db-c68a7c066360@t23g2000yqt.googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!t23g2000yqt.googlegroups.com!not-for-mail
References: <77c00642-1b59-418a-a26a-fb99b3c32e81@g11g2000yqe.googlegroups.com> <b630f510-d64c-4eaf-8212-0f1b80d11a1f@v25g2000yqk.googlegroups.com> <4d45db34-b9c3-4939-8a81-6e6afe9b507c@f35g2000yqd.googlegroups.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!t23g2000yqt.googlegroups.com!not-for-mail
From: Łukasz Rekucki <lrekucki@gmail.com>
Newsgroups: pl.comp.lang.python
Subject: Re: jak sensownie polaczyc watki z rekurencja?
Date: Thu, 25 Feb 2010 09:14:29 -0800 (PST)
Organization: http://groups.google.com
Lines: 84
Message-ID: <1caeb989-aee5-4af8-81db-c68a7c066360@t23g2000yqt.googlegroups.com>
References: <77c00642-1b59-418a-a26a-fb99b3c32e81@g11g2000yqe.googlegroups.com>  <b630f510-d64c-4eaf-8212-0f1b80d11a1f@v25g2000yqk.googlegroups.com>  <4d45db34-b9c3-4939-8a81-6e6afe9b507c@f35g2000yqd.googlegroups.com>
NNTP-Posting-Host: 212.76.62.207
Mime-Version: 1.0
X-Trace: posting.google.com 1267118069 9662 127.0.0.1 (25 Feb 2010 17:14:29 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 25 Feb 2010 17:14:29 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: t23g2000yqt.googlegroups.com; posting-host=212.76.62.207;  posting-account=5fpXZwoAAADF-CUdCuJ-FpYxG8__-wRx
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)  AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.89 Safari/532.5,gzip(gfe),gzip(gfe)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit