Comments (7)
Fixed.
It's conflicting because crython use function name as unique identity.
Here's the code that working:
for ex, speech in simple_map.items():
def wrapped_func(_): # add wrapped function to fix lexical closures
def _f():
print(simple_map[ex])
return _f
func = wrapped_func(ex)
func.__name__ = ex
crython.job(expr=ex)(func)
Or the simple version:
for ex, speech in simple_map.items():
func = (lambda _: lambda: print(simple_map[_]))(ex)
func.__name__ = ex
crython.job(expr=ex)(func)
from crython.
Why not expose a separate, non-decorator-based api for scheduling predefined functions?
from crython.
@micimize thank you for that, I would make adjustments
from crython.
@tomzhu6066
Yeah, I thought about this one a bit. I would like to add a name
parameter to the @job
decorator. However, the corner I've painted myself into is how the @job
decorator will pass args/kwargs to the decorated function. If @job
takes a name
parameter, it means that this will collide with any decorated functions that also take a name
keyword argument. I have considered _
prefixing but find it ugly.
I'll think about this some more and try and come up with a clean way to do:
- Dynamically set the name of a
job
and fallback to the function name when not specified. - Does not clash with decorated function args/kwargs
- Maintains backwards compat
Ideally the situation would be something like:
jobs = [
dict(name='check_thing_every_second', expr='*/1 * * * * * *'),
dict(name='check_thing_every_two_seconds', expr='*/2 * * * * * *')
]
for job in jobs:
@crython.job(name=job['name'], expr=job['expr'])
def func():
print('This function is run as part of two jobs; scheduled for one and two second intervals')
from crython.
@ahawker what do you think about @micimize' suggestion?
from crython.
@pariola Here's a wrapper util I'm using in python 3.
import typing as t
from logging import getLogger as get_logger
import crython
log = get_logger(__name__)
def schedule_job(job: t.Callable[..., t.Any], schedule: t.List[str]):
"""Schedule a given ``job`` with a list of crython compatible crontab configs
Useful for deploying periodic jobs
"""
log.info(f"running {job.__name__} with the schedules {schedule}")
for index, expr in enumerate(schedule):
def wrapped_job(*args, **kwarg):
"wrap and rename the job to prevent name collisions"
try:
return job(*args, **kwarg)
except Exception as e:
log.exception(f"Exception thrown during job {job.__name__}")
pass
wrapped_job.__name__ = "%s_%i" % (job.__name__, index)
crython.job(expr=expr)(wrapped_job)
crython.start()
crython.join()
# usage
schedule_job(my_job, ['@reboot', '*/5 * * * * * *'])
Tangentially, as an OS maintainer, I'd advise against phrasing requests the way you just did. To me, it reads like a command, and comes off as entitled. Prefer phrasing like "What do you think of this suggestion?", "Do you have time to implement this".
from crython.
Related Issues (20)
- How to update job HOT 1
- Request for updating pypi version to 0.0.8 HOT 2
- multi threading HOT 1
- Fix 'ctx' examples in README
- Add Python 3.7 Support HOT 1
- using minute= only assumes second='*' HOT 1
- Keep the main script alive, threads with I/O HOT 3
- Feature: Block on global CronTab thread HOT 1
- Using range HOT 2
- How to change the logging level HOT 1
- crython is not working HOT 1
- Expression "@reboot" does not work HOT 6
- Full Windows Support
- Exceptions being suppressed HOT 2
- Reserved keywords are mapped to wrong expressions
- Dependency on six is not explicit
- Week day doesn't work properly
- pyup.io: Initial Update
- Add isort support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crython.