PyTask from document will not run

I am not sure if this is a bug,
But your example is not working for PyTask

task4 = Firework(PyTask(func=‘time.sleep’,args=[5]))

set up the LaunchPad and reset it

launchpad = LaunchPad()
launchpad.reset(’’, require_password=False)
fireworker = Firework(task4)
launchpad.add_wf(fireworker)
launch_rocket(launchpad)


Traceback (most recent call last):
File “/core/apps/fw-master/fireworksMPC/testComplex.py”, line 32, in
launch_rocket(launchpad)
File “/cube/api/py-3.9.1/lib/python3.9/site-packages/fireworks/core/rocket_launcher.py”, line 58, in launch_rocket
rocket_ran = rocket.run(pdb_on_exception=pdb_on_exception)
File “/cube/api/py-3.9.1/lib/python3.9/site-packages/fireworks/core/rocket.py”, line 145, in run
m_fw, launch_id = lp.checkout_fw(self.fworker, launch_dir, self.fw_id)
File “/cube/api/py-3.9.1/lib/python3.9/site-packages/fireworks/core/launchpad.py”, line 1478, in checkout_fw
self._upsert_fws([m_fw])
File “/cube/api/py-3.9.1/lib/python3.9/site-packages/fireworks/core/launchpad.py”, line 1659, in _upsert_fws
self.fireworks.find_one_and_replace({“fw_id”: fw.fw_id}, fw.to_db_dict(), upsert=True)
File “/cube/api/py-3.9.1/lib/python3.9/site-packages/fireworks/core/firework.py”, line 363, in to_db_dict
m_dict = self.to_dict()
File “/cube/api/py-3.9.1/lib/python3.9/site-packages/fireworks/utilities/fw_serializers.py”, line 145, in _decorator
m_dict = func(self, *args, **kwargs)
File “/cube/api/py-3.9.1/lib/python3.9/site-packages/fireworks/core/firework.py”, line 317, in to_dict
spec["_tasks"] = [t.to_dict() for t in self.tasks]
File “/cube/api/py-3.9.1/lib/python3.9/site-packages/fireworks/core/firework.py”, line 317, in
spec["_tasks"] = [t.to_dict() for t in self.tasks]
AttributeError: ‘dict’ object has no attribute ‘to_dict’

Tried on 3.9.1 and 3.10.0 same results
FW version: 1.9.8
Mongo has wf, no issue there

This is part of
@recursive_serialize
def to_dict(self):

I am not sure maybe I am not doing something right here?

Hi Anubhav,

PyTask is definitely failing here:

self.fireworks.find_one_and_replace({‘fw_id’: fw.fw_id},
fw.to_db_dict(),
upsert=True)

so:
def to_dict(self):

spec[’_tasks’] = [t.to_dict() for t in self.tasks]

t is already a dict and spec[’_tasks’] is trying to convert dict to dict failing miserably …

{‘spec’: {’_tasks’: [:{‘func’: ‘time.sleep’, ‘args’: [5], ‘_fw_name’: ‘PyTask’}]}, ‘fw_id’: -1, ‘created_on’: datetime.datetime(2022, 1, 16, 20, 20, 16, 789436), ‘updated_on’: datetime.datetime(2022, 1, 16, 20, 20, 16, 789437), ‘name’: ‘Unnamed FW’}

so I changed this code to debug a bit:

@recursive_serialize
def to_dict(self):
# put tasks in a special location of the spec
spec = self.spec
spec[’_tasks’] = list()
for t in self.tasks:
print(t)
if isinstance(t, dict):
spec[’_tasks’].append(t)
else:
td = t.to_dict()
spec[’_tasks’].append(td)

    ##spec['_tasks'] = [t.to_dict() for t in self.tasks]
    m_dict = {'spec': spec, 'fw_id': self.fw_id, 'created_on': self.created_on,
              'updated_on': self.updated_on}

    # only serialize these fields if non-empty
    if len(list(self.launches)) > 0:
        m_dict['launches'] = self.launches

    if len(list(self.archived_launches)) > 0:
        m_dict['archived_launches'] = self.archived_launches

    # keep export of new FWs to files clean
    if self.state != 'WAITING':
        m_dict['state'] = self.state

    m_dict['name'] = self.name

    return m_dict

Now firework is working correctly, meaning rocket can finish…
problem at this point is:

2022-01-16 15:28:50,762 INFO Rocket finished
Traceback (most recent call last):
File “/cube/api/py-3.9.7/lib/python3.9/site-packages/fireworks/core/rocket.py”, line 248, in run
l_logger.log(logging.INFO, "Task started: s." t.fw_name)
AttributeError: ‘dict’ object has no attribute ‘fw_name’

It does not affect me anymore but it should be fixed.
I think that logic is broken somewhere as method to_dict is constantly being used over and over again.
Obviously, I did simple thing and went around no eval of input to some eval to pass ugly, but it would be nice if you can explain rationale on this or possibly if documentation is stale for PyTask and we shoudl do this differently.

Regards, --sasha