0

I try to use monkey patch these days. I found that if a code is monkey patched, it will produce different thread.ident. does it means monkey patch start up multiple threading, rather than multiple coroutines?

If it start up multiple threading, thus it brings race condition, doesn't it ?

Please help, thank you in advance.

there are the two test code I used.

without monkey_patch(), blocked code, but print the same thread ids.

#!/usr/bin/env python
# encoding: utf-8

import eventlet
# comment monkey_patch
# eventlet.monkey_patch()
import time
import thread

workers=4
jobs = 5
record = {}
pool = eventlet.greenpool.GreenPool()

def do_work(index):
    global record
    record[index] = record.get(index, 0) + 1
    # data = "worker=%s: %s \n" % (index, record[index])
    # print data
    # eventlet.greenthread.sleep(1)

    # block
    time.sleep(1)

def work(worker, jobs):
    for x in xrange(0, jobs):
        # print same thread id
        print "worker in thread %s" % thread.get_ident()
        do_work(worker)

for i in xrange(workers):
    print 'worker: %s do %s jobs'% (i, jobs)
    pool.spawn(work, i, jobs)
pool.waitall()

output is like below:

[root@172-18-211-195 shengping]# python test_mky.py
worker: 0 do 5 jobs
worker: 1 do 5 jobs
worker: 2 do 5 jobs
worker: 3 do 5 jobs
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992
worker in thread 140221126620992

with monkey_patch(), unblocked code, but print the different thread ids.

#!/usr/bin/env python
# encoding: utf-8

import eventlet
# comment monkey_patch
eventlet.monkey_patch()
import time
import thread

workers=4
jobs = 5
record = {}
pool = eventlet.greenpool.GreenPool()

def do_work(index):
    global record
    record[index] = record.get(index, 0) + 1
    # data = "worker=%s: %s \n" % (index, record[index])
    # print data
    # eventlet.greenthread.sleep(1)

    # block
    time.sleep(1)

def work(worker, jobs):
    for x in xrange(0, jobs):
        # print same thread id
        print "worker in thread %s" % thread.get_ident()
        do_work(worker)

for i in xrange(workers):
    print 'worker: %s do %s jobs'% (i, jobs)
    pool.spawn(work, i, jobs)
pool.waitall()

output is like below (as you can see each workers with different threading id):

[root@172-18-211-195 shengping]# python test_mky.py
worker: 0 do 5 jobs
worker: 1 do 5 jobs
worker: 2 do 5 jobs
worker: 3 do 5 jobs
worker in thread 27456048
worker in thread 27456208
worker in thread 27456368
worker in thread 27456528
worker in thread 27456048
worker in thread 27456208
worker in thread 27456368
worker in thread 27456528
worker in thread 27456048
worker in thread 27456208
worker in thread 27456368
worker in thread 27456528
worker in thread 27456048
worker in thread 27456208
worker in thread 27456368
worker in thread 27456528
worker in thread 27456048
worker in thread 27456208
worker in thread 27456368
worker in thread 27456528
cosz3
  • 359
  • 4
  • 15

1 Answers1

0
  • Patched threading module makes its operations work on green threads
  • Eventlet avoids OS threads unless you use tpool
  • Still it's not clear what is your intent or problem
temoto
  • 5,394
  • 3
  • 34
  • 50
  • It seems that monkeypatch makes the threading id of green threadings (coroutines) different from each other. Without monkeypatch the threading id of green threadings (coroutines) are all the same. I thought the (green threadings) coroutines should always be in the same threading, thus the threading ids of green threadings (coroutines) should be the same. I think I have figured out my question. The green threading is still coroutines, they are just seems like different threading with different threading ids (since they are patched). – cosz3 May 08 '18 at 07:29