[R2] Essaie de Task
trigram17-Feb-2011/17:51:17+1:00
Voici un première ébauche de mon task :

%obj.r

REBOL [
  title: "OBJ"
  file: %obj.r
]

make-obj: func [
  "Créé un objet en sauvant son nom dedans."
  'name "Nom de l'objet à créer."
  obj "Objet de base à instancier."
  spec "extension de l'objet de base."
] [
  set name make obj append reduce [to-set-word 'obj-name to-string name] spec
]

save-obj: func [
  "Sauvegarde un objet selon son propre nom."
  'obj "Objet à sauvegarder."
  /dir directory
  /local name file
] [
  name: any [all [word? obj  object? get obj  get in get obj 'obj-name] join "objet" random 10000]
  file: copy ""
  if dir [file: join directory file]
  file: to-file rejoin [file name ".r"]
  make-dir/deep to-file directory
  save file get obj
]

load-obj: func [
  "Recharge un objet et l'intanci selon son propre nom s'il en a un."
  file "Nom du fichier à charger."
  /bindto "Spécifier le contexte où binder la liste de l'objet chargé."
  ctx "Nom de l'objet (contexte) ou binder la liste de l'objet chargé."
  /local obj
] [
  if exists? file [
  obj: do load file
  if all [in obj 'obj-name  set to-word get in obj 'obj-name obj] [
    if all [bindto  object! ctx] [bind obj/list ctx]
  ]
 ]
 obj
]


%task.r

REBOL [
  title: "TASK"
  file: %task.r
]

do %obj.r
debug: true
task: make object! [
  obj-name: copy ""
  task-dir: "task/"
  list: copy []
  add: funct [
  	"Ajoute à la liste."
  	t [block!] "Elément ou code à ajouter la liste."
  	/bindto "Spécifier le contexte où binder la liste de l'objet créé."
	  ctx "Nom de l'objet (contexte) ou binder la liste de l'objet créé."
  ] [
    append list t
	  if all [bindto object! ctx] [bind list ctx]
	  list
  ]
  save: does [
	  save-obj/dir self task-dir
  ]
  run: has [task-file [file!]] [
    task-file: to-file rejoin [task-dir obj-name ".tsk"]
    attempt [delete task-file]
    do list
    write task-file "OK"
  ]
  launch: does [
    save
    call/show join form to-local-file system/options/boot [" %launch-task.r " obj-name]
  ]
  finished: does [
    exists? to-file rejoin [task-dir obj-name ".tsk"]
  ]
]

load-task: funct [task-name [string!]] [
  load-obj to-file rejoin ["task/" task-name ".r"]
]


%launch-task.r

REBOL [
  title: "LAUNCH-TASK"
  file: %launch-task.r
]

do %task.r
if value? 'debug [if debug [print rejoin ["Launch task = " system/script/args]]]
launch-task: load-task system/script/args
launch-task/run
if value? 'debug [if debug [ask "Task terminated !"]]


%demo-task.r

REBOL [
  title: "DEMO-TASK"
  file: %demo-task.r
]

do %task.r

make-obj task1 task []
task1/add [c: 0 c: c + 68]
task1/add [print c]
task1/add [wait 00:00:05]
task1/add [print "1 finished !"]

make-obj task2 task []
task2/add [a: 0 a: a + 28]
task2/add [print a]

task1/launch
task2/launch

print "Waiting for tasks to finish..."
t1: t2: false
while [not (t1 and t2)] [
  if not t1 [t1: task1/finished if t1 [print "Task 1 is finished"]]
  if not t2 [t2: task2/finished if t2 [print "Task 2 is finished"]]
]
print "All Task are finished !"
halt


C'est sûrement maladroit, mais cela me permettra de simuler du multi-tâche de manière simple.
Notamment, pour le téléchargement des éléments d'une page Web en parallèle.

Nico

Login required to Post.


Powered by RebelBB and REBOL 2.7.8.4.2