{"action":"created","issue":{"url":"https://api.github.com/repos/mafredri/zsh-async/issues/4","repository_url":"https://api.github.com/repos/mafredri/zsh-async","labels_url":"https://api.github.com/repos/mafredri/zsh-async/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/mafredri/zsh-async/issues/4/comments","events_url":"https://api.github.com/repos/mafredri/zsh-async/issues/4/events","html_url":"https://github.com/mafredri/zsh-async/issues/4","id":155396822,"number":4,"title":"Changing a variable in a worker","user":{"login":"andjscott","id":3648487,"avatar_url":"https://avatars.githubusercontent.com/u/3648487?v=3","gravatar_id":"","url":"https://api.github.com/users/andjscott","html_url":"https://github.com/andjscott","followers_url":"https://api.github.com/users/andjscott/followers","following_url":"https://api.github.com/users/andjscott/following{/other_user}","gists_url":"https://api.github.com/users/andjscott/gists{/gist_id}","starred_url":"https://api.github.com/users/andjscott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andjscott/subscriptions","organizations_url":"https://api.github.com/users/andjscott/orgs","repos_url":"https://api.github.com/users/andjscott/repos","events_url":"https://api.github.com/users/andjscott/events{/privacy}","received_events_url":"https://api.github.com/users/andjscott/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-18T01:11:45Z","updated_at":"2016-05-18T06:17:05Z","closed_at":null,"body":"Is it possible to change a variable used by jobs in a worker? It seems that a possible way is to stop a worker and start it up again. The use case is a variable changes outside the worker and the worker needs to be able to read the new value. The code below shows that assignments don't stick between jobs \r\n```\r\n#!/bin/zsh\r\n\r\nsource ./async.zsh\r\nasync_init\r\n\r\nset_val() {\r\n\tval=$1\r\n\techo \"set val: $val\"\r\n}\r\n\r\nview_val() {\r\n\techo \"view val: $val\"\r\n}\r\n\r\nasync_start_worker my_worker -n\r\nasync_register_callback my_worker completed_callback\r\n\r\n# Create a callback function to process results\r\nCOMPLETED=0\r\ncompleted_callback() {\r\n    COMPLETED=$(( COMPLETED + 1 ))\r\n    print $3\r\n}\r\n\r\n\r\nview_val\r\nset_val 100\r\nview_val\r\n\r\necho \"======================\"\r\n\r\nasync_job my_worker view_val\r\nasync_job my_worker set_val 200\r\nasync_job my_worker view_val\r\n\r\nwhile (( COMPLETED < 3 )); do\r\n    print \"Waiting...\"\r\n    sleep 0.1\r\ndone\r\n\r\necho \"======================\"\r\n\r\nCOMPLETED=0\r\nasync_stop_worker my_worker -n\r\nval=300\r\nasync_start_worker my_worker -n\r\nasync_register_callback my_worker completed_callback\r\nasync_job my_worker view_val\r\n\r\nwhile (( COMPLETED < 1 )); do\r\n    print \"Waiting...\"\r\n    sleep 0.1\r\ndone\r\n```\r\nDisplays:\r\n```\r\nview val:\r\nset val: 100\r\nview val: 100\r\n======================\r\nWaiting...\r\nview val:\r\nset val: 200\r\nview val:\r\n======================\r\nWaiting...\r\nview val: 300\r\n\r\n\r\n```\r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/mafredri/zsh-async/issues/comments/219934967","html_url":"https://github.com/mafredri/zsh-async/issues/4#issuecomment-219934967","issue_url":"https://api.github.com/repos/mafredri/zsh-async/issues/4","id":219934967,"user":{"login":"mafredri","id":147409,"avatar_url":"https://avatars.githubusercontent.com/u/147409?v=3","gravatar_id":"","url":"https://api.github.com/users/mafredri","html_url":"https://github.com/mafredri","followers_url":"https://api.github.com/users/mafredri/followers","following_url":"https://api.github.com/users/mafredri/following{/other_user}","gists_url":"https://api.github.com/users/mafredri/gists{/gist_id}","starred_url":"https://api.github.com/users/mafredri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mafredri/subscriptions","organizations_url":"https://api.github.com/users/mafredri/orgs","repos_url":"https://api.github.com/users/mafredri/repos","events_url":"https://api.github.com/users/mafredri/events{/privacy}","received_events_url":"https://api.github.com/users/mafredri/received_events","type":"User","site_admin":false},"created_at":"2016-05-18T06:17:05Z","updated_at":"2016-05-18T06:17:05Z","body":"The reason you're not seeing `val` inside your worker is that each worker only has access to the global environment as it is when it was started, `zpty` instances essentially create a copy of the current state, so updates are not propagated.\r\n\r\nThe way I recommend using `async` is to have your job functions accept and return parameters (this still means there's no way to communicate to a running job, other than to kill it).\r\n\r\n1. Set function prints the newly set value\r\n2. Callback function sees that the set function has finished, and updates the variable on the global scope\r\n3. New value is passes to the view job through a parameter\r\n\r\nI've updated your example slightly to illustrate this:\r\n\r\n```\r\n#!/usr/bin/env zsh\r\n\r\nsource ./async.zsh\r\nasync_init\r\n\r\nset_val() {\r\n\tval=$1\r\n    print $val\r\n}\r\n\r\nview_val() {\r\n\tlocal val=$1\r\n    echo \"view val: $val\"\r\n}\r\n\r\nasync_start_worker my_worker -n\r\nasync_register_callback my_worker completed_callback\r\n\r\n# Create a callback function to process results\r\nCOMPLETED=0\r\ncompleted_callback() {\r\n\tcase $1 in\r\n\t\tset_val) val=$3;; # set val to the output of set_val\r\n\tesac\r\n    COMPLETED=$(( COMPLETED + 1 ))\r\n    print $3\r\n}\r\n\r\nset_val 100\r\n\r\nasync_job my_worker view_val $val\r\nasync_job my_worker set_val 200\r\n\r\nsleep 0.1 # waiting for set_val to return so that global var gets set\r\n\r\nasync_job my_worker view_val $val\r\n\r\nwhile (( COMPLETED < 3 )); do\r\n    print \"Waiting...\"\r\n    sleep 0.1\r\ndone\r\n```\r\n\r\nDoes this help with your problem? You should essentially synchronize your application through the callback function which is provided with information about which job has completed."}}