shoom013 commited on
Commit
46540bf
·
verified ·
1 Parent(s): 7b75bf9

Update Consumer.py

Browse files
Files changed (1) hide show
  1. Consumer.py +27 -17
Consumer.py CHANGED
@@ -10,6 +10,20 @@ from FoxDot import *
10
  API_URL = "https://houseofplaying.com/hop/api.php"
11
  POLL_INTERVAL = 1 # Seconds
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Global dictionary to store active tasks and their cancellation tokens (or just the futures)
14
  active_tasks = {}
15
 
@@ -30,8 +44,6 @@ def update_server(uid, output, status):
30
  print(f"Error updating server: {e}")
31
 
32
  def stop(uid):
33
- #global loop
34
- #global active_tasks
35
  """Stops the execution of the cell with the given uid."""
36
  if uid in active_tasks:
37
  print(f"Stopping task {uid}...")
@@ -45,8 +57,6 @@ def stop(uid):
45
 
46
  def stopall():
47
  """Stops all running executions."""
48
- #global loop
49
- #global active_tasks
50
  count = 0
51
  for uid in list(active_tasks.keys()):
52
  if active_tasks[uid] != asyncio.current_task():
@@ -69,7 +79,7 @@ def stopall():
69
  print("Starting new event loop...")
70
  loop=asyncio.run(main())
71
  loop.run_forever()
72
-
73
  except KeyboardInterrupt:
74
  print("Stopping consumer...")
75
 
@@ -77,17 +87,17 @@ def stopall():
77
 
78
  def execute_code_sync(code, uid):
79
  # Capture stdout
80
- #global loop
81
- #global active_tasks
82
  stdout_capture = i_o.StringIO()
83
  status = "completed"
84
  output = ""
85
 
86
  # Inject stop functions into locals
87
  local_scope = locals()
88
- #local_scope['stop'] = stop
89
- #local_scope['stopall'] = stopall
90
-
 
 
91
  try:
92
  with contextlib.redirect_stdout(stdout_capture):
93
  # Execute the code
@@ -131,24 +141,24 @@ async def process_task(uid, code):
131
 
132
  async def main():
133
  print(f"Starting FoxDot Consumer (Async)... Polling {API_URL}")
134
-
135
  while True:
136
  # Move polling to thread to avoid blocking loop
137
  task_data = await asyncio.to_thread(poll_server)
138
-
139
  if task_data:
140
  uid = task_data.get('uid')
141
  code = task_data.get('code')
142
  print(f"Received task {uid}")
143
-
144
  # Create async task
145
  task = asyncio.create_task(process_task(uid, code))
146
  active_tasks[uid] = task
147
  if uid in active_tasks:
148
- active_tasks[uid].cancel()
149
 
150
  await asyncio.sleep(POLL_INTERVAL)
151
-
152
  if __name__ == "__main__":
153
  try:
154
  # Check if an event loop is already running
@@ -165,7 +175,7 @@ if __name__ == "__main__":
165
  else:
166
  print("Starting new event loop...")
167
  loop = asyncio.run(main())
168
-
169
- loop.run_forever()
170
  except KeyboardInterrupt:
171
  print("Stopping consumer...")
 
10
  API_URL = "https://houseofplaying.com/hop/api.php"
11
  POLL_INTERVAL = 1 # Seconds
12
 
13
+ def change_key(amt = 3, scl="minor", duration=4, flag=True):
14
+ Root.default = ((Root.default+amt) % 14)-5
15
+ print(f"Key changed to {Root.default}")
16
+ Scale.default = scl
17
+ if flag:
18
+ Clock.future(duration, change_key, args=[amt, scl, duration])
19
+
20
+ def change_cycle(notes, scales, plen, flag=True):
21
+ if Clock.now() % plen) == 0:
22
+ Scale.default = scales[(Clock.now()/plen) % len(notes)]
23
+ Root.default = notes[(Clock.now()/plen) % len(notes)]
24
+ if flag:
25
+ change_cycle(notes, scales, plen)
26
+
27
  # Global dictionary to store active tasks and their cancellation tokens (or just the futures)
28
  active_tasks = {}
29
 
 
44
  print(f"Error updating server: {e}")
45
 
46
  def stop(uid):
 
 
47
  """Stops the execution of the cell with the given uid."""
48
  if uid in active_tasks:
49
  print(f"Stopping task {uid}...")
 
57
 
58
  def stopall():
59
  """Stops all running executions."""
 
 
60
  count = 0
61
  for uid in list(active_tasks.keys()):
62
  if active_tasks[uid] != asyncio.current_task():
 
79
  print("Starting new event loop...")
80
  loop=asyncio.run(main())
81
  loop.run_forever()
82
+
83
  except KeyboardInterrupt:
84
  print("Stopping consumer...")
85
 
 
87
 
88
  def execute_code_sync(code, uid):
89
  # Capture stdout
 
 
90
  stdout_capture = i_o.StringIO()
91
  status = "completed"
92
  output = ""
93
 
94
  # Inject stop functions into locals
95
  local_scope = locals()
96
+ local_scope['stop'] = stop
97
+ local_scope['stopall'] = stopall
98
+ local_scope['change_key'] = change_key
99
+ local_scope['change_cycle'] = change_cycle
100
+
101
  try:
102
  with contextlib.redirect_stdout(stdout_capture):
103
  # Execute the code
 
141
 
142
  async def main():
143
  print(f"Starting FoxDot Consumer (Async)... Polling {API_URL}")
144
+
145
  while True:
146
  # Move polling to thread to avoid blocking loop
147
  task_data = await asyncio.to_thread(poll_server)
148
+
149
  if task_data:
150
  uid = task_data.get('uid')
151
  code = task_data.get('code')
152
  print(f"Received task {uid}")
153
+
154
  # Create async task
155
  task = asyncio.create_task(process_task(uid, code))
156
  active_tasks[uid] = task
157
  if uid in active_tasks:
158
+ active_tasks[uid].cancel()
159
 
160
  await asyncio.sleep(POLL_INTERVAL)
161
+
162
  if __name__ == "__main__":
163
  try:
164
  # Check if an event loop is already running
 
175
  else:
176
  print("Starting new event loop...")
177
  loop = asyncio.run(main())
178
+
179
+ loop.run_forever()
180
  except KeyboardInterrupt:
181
  print("Stopping consumer...")