python - How monkey_patch(time=True) affects eventlet.spawn? -
normally, when using greenthread, can write code as:
def myfun(): print "in func" eventlet.spawn(myfunc) eventlet.sleep(0) #then myfunc() triggered thread. but after use money_patch(time=true), can change code as:
eventlet.monkey_patch(time=true) eventlet.spawn(myfunc) # myfunc called why dont need call eventlet.sleep(0) time?
and after write own sleep function:
def my_sleep(seconds): print "oh, god, own..." and set sleep attr of built-in time module my_sleep func, find my_sleep func called many times lot of outputs. can see 1 debug-thread in eclipse, did not call my_sleep func.
so, conclusion is, sleep function called continually default, , think authors of eventlet know this, developed monkey_patch() function. rigth?
according answer of @temoto, cpython cannot reproduce same result. thinks should add message introduce how find interesting thing.(why did not add message @ first time, cause input many words in not easy, , english not good.^^)
i find thing when remote-debug openstack code using eclipse.
in nova/network/model.py, function written this:
class networkinfoasyncwrapper(networkinfo): """wrapper around networkinfo allows retrieving networkinfo in async manner. allows 1 start querying network information before know need it. if have long-running operation, allows network model retrieval occur in background. when need data, ensure async operation has completed. example: def allocate_net_info(arg1, arg2) return call_neutron_to_allocate(arg1, arg2) network_info = networkinfoasyncwrapper(allocate_net_info, arg1, arg2) [do long running operation -- real network_info retrieved in background] [do network_info] """ def __init__(self, async_method, *args, **kwargs): self._gt = eventlet.spawn(async_method, *args, **kwargs) methods = ['json', 'fixed_ips', 'floating_ips'] method in methods: fn = getattr(self, method) wrapper = functools.partial(self._sync_wrapper, fn) functools.update_wrapper(wrapper, fn) setattr(self, method, wrapper) when first debug function, after exeute
self._gt = eventlet.spawn(async_method, *args, **kwargs)
the call-back function async_method exeuted @ once. please remember, thread, should triggered eventlet.sleep(0).
but didnt find code call sleep(0), if sleep perhaps called eclipse, in real world(not-debug world), triggered it?
tl;dr: eventlet api not require sleep(0) start green thread. spawn(fun) start function time in future, including right now. should call sleep(0) ensure starts right now, , it's better use explicit synchronization, event or semaphore.
i can't reproduce behavior using cpython 2.7.6 , 3.4.3, eventlet 0.17.4 in either ipython or pure python console. it's eclipse calling time.sleep in background.
monkey_patch introduced shortcut running through (and third party) code , replacing time.sleep -> eventlet.sleep, , similar os, socket, etc modules. it's not related eclipse (or else) repeating time.sleep calls.
but interesting observation, thank you.
Comments
Post a Comment