On Sun, Jun 21, 2015 at 5:55 AM, anu sree <anusree....@gmail.com> wrote:

> Thanks naufal and Krace.
>
> I have tried following code. I have given gevent.sleep(0.1), that means
> each greenlet let others to execute 0.1 secoond. But here each greenlet
> waiting for others to complete, why?  Is it because of greenlet.joinall ?
>

Gevent has the hub/scheduler. The whole job is to schedule the tasks. AFAIK,
the hub uses queue to schedule them. If the task has a sleep [1], task is
enqueued for later execution [2].
By saying sleep, gevent puts task out of execution and doesn't guarantee
execution once time is elapsed.


>
> Here I have created 3 greenlet threads (A, B, C). I think, A is the parent
> greenlet, right ? Once C get finished control should goes to B, right ?
> But here it going to A.
> Please check the ouput.
>
> import gevent
>
> def task(num):
>     thread_id = id(gevent.getcurrent())
>     print "Start: ", num, thread_id
>
>     for i in range(100000001):
>         if i == 100000000:
>             print num + str(i)
>
>     gevent.sleep(0.1)
>
>     for i in range(100000001):
>         if i == 100000000:
>             print num + str(i)
>
>     print "End: ", num, thread_id
>
> threads = [gevent.spawn(task, x) for x in ['A', 'B', 'C']]
> gevent.joinall(threads)
>
>
> Output
> =======
>
> Start:  A 139984038735696
> A100000000
> Start:  B 139984008421616
> B100000000
> Start:  C 139984008421776
> C100000000
> A100000000
> End:  A 139984038735696
> B100000000
> End:  B 139984008421616
> C100000000
> End:  C 139984008421776
>

Parent in gevent context is Hub(run inside an event loop). Like unix
process concept, gevent doesn't have parent process and child process.
Let's say if a task is spawn inside gevent task, they are still individual
tasks.

In [24]: def task(langs):
    print "inside task", gevent.getcurrent(), gevent.getcurrent().parent
    def echo(msg):
        print msg, gevent.getcurrent(), gevent.getcurrent().parent
    tasks = [gevent.spawn(echo, lang) for lang in langs]
    gevent.joinall(tasks)
   ....:

In [25]: gevent.joinall([gevent.spawn(task, langs) for langs in [('Python',
'Go'), ('Lisp', 'Clojure')]])
inside task <Greenlet at 0x1025269b0: task(('Python', 'Go'))> <Hub at
0x1025262d0 select default pending=0 ref=1>
inside task <Greenlet at 0x102526f50: task(('Lisp', 'Clojure'))> <Hub at
0x1025262d0 select default pending=0 ref=2>
Python <Greenlet at 0x102526cd0: echo('Python')> <Hub at 0x1025262d0 select
default pending=0 ref=3>
Go <Greenlet at 0x102526d70: echo('Go')> <Hub at 0x1025262d0 select default
pending=0 ref=3>
Lisp <Greenlet at 0x1025265f0: echo('Lisp')> <Hub at 0x1025262d0 select
default pending=0 ref=3>
Clojure <Greenlet at 0x102526c30: echo('Clojure')> <Hub at 0x1025262d0
select default pending=0 ref=3>

_______________________________________________
> BangPypers mailing list
> BangPypers@python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>


[1]: https://github.com/gevent/gevent/blob/master/gevent/hub.py#L80
[2]:
https://github.com/gevent/gevent/blob/1599210203836560698a5facac015c31735cac3d/gevent/corecffi.py#L402

-- 

*Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
Torvaldshttp://kracekumar.com <http://kracekumar.com>*
_______________________________________________
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers

Reply via email to