Giter Site home page Giter Site logo

Comments (7)

tommyyz avatar tommyyz commented on August 25, 2024 1

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.

micimize avatar micimize commented on August 25, 2024 1

Why not expose a separate, non-decorator-based api for scheduling predefined functions?

from crython.

pariola avatar pariola commented on August 25, 2024 1

@micimize thank you for that, I would make adjustments

from crython.

ahawker avatar ahawker commented on August 25, 2024

@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.

pariola avatar pariola commented on August 25, 2024

@ahawker what do you think about @micimize' suggestion?

from crython.

micimize avatar micimize commented on August 25, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.