Here are the examples of the python api sys._getframe.f_code.co_flags taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
4 Examples
3
Example 1
def blocking(func):
'''
Decorator indicating that a function performs a blocking operation.
If called from synchronous Python code, the function runs normally.
However, if called from a coroutine, curio arranges for it to run
in a thread.
'''
from . import workers
@wraps(func)
def wrapper(*args, **kwargs):
if sys._getframe(1).f_code.co_flags & 0x80:
return workers.run_in_thread(func, *args, **kwargs)
else:
return func(*args, **kwargs)
return wrapper
3
Example 2
def cpubound(func):
'''
Decorator indicating that a function performs a cpu-intensive operation.
If called from synchronous Python code, the function runs normally.
However, if called from a coroutine, curio arranges for it to run
in a process.
'''
from . import workers
@wraps(func)
def wrapper(*args, **kwargs):
if sys._getframe(1).f_code.co_flags & 0x80:
# The use of wrapper in the next statement is not a typo.
return workers.run_in_process(wrapper, *args, **kwargs)
else:
return func(*args, **kwargs)
return wrapper
3
Example 3
Project: curio Source File: meta.py
def sync_only(func):
'''
Decorator indicating that a function is only valid in synchronous code.
'''
@wraps(func)
def wrapper(*args, **kwargs):
if sys._getframe(1).f_code.co_flags & 0x80:
raise SyncIOError('{} may only be used in synchronous code'.format(func.__name__))
else:
return func(*args, **kwargs)
return wrapper
0
Example 4
Project: curio Source File: meta.py
def awaitable(syncfunc):
'''
Decorator that allows an asynchronous function to be paired with a
synchronous function in a single function call. The selection of
which function executes depends on the calling context. For example:
def spam(sock, maxbytes): (A)
return sock.recv(maxbytes)
@awaitable(spam) (B)
async def spam(sock, maxbytes):
return await sock.recv(maxbytes)
In later code, you could use the spam() function in either a synchronous
or asynchronous context. For example:
def foo():
...
r = spam(s, 1024) # Calls synchronous function (A) above
...
async def bar():
...
r = await spam(s, 1024) # Calls async function (B) above
...
'''
def decorate(asyncfunc):
if inspect.signature(syncfunc) != inspect.signature(asyncfunc):
raise TypeError('%s and async %s have different signatures' %
(syncfunc.__name__, asyncfunc.__name__))
@wraps(asyncfunc)
def wrapper(*args, **kwargs):
if sys._getframe(1).f_code.co_flags & 0x80:
return asyncfunc(*args, **kwargs)
else:
return syncfunc(*args, **kwargs)
return wrapper
return decorate