-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathspawner.py
57 lines (47 loc) · 1.67 KB
/
spawner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gevent
import logging
import os
import importlib
from .sandboxer import Sandboxer
class Spawner(object):
def __init__(self, site, io):
self.site = site
self.log = logging.getLogger("Spawner:%s" % self.site.address_short)
self.threads = []
self.io = io
io["spawner"] = self
def spawn(self, ext, code):
# Find correct transpiler for this file, based on extension
transpiler = self.findTranspiler(ext)
if transpiler is None:
self.log.debug("No transpiler for 0background.%s" % ext)
return False
# Transpile
self.log.debug("Transpiling 0background.%s" % ext)
try:
transpiled = transpiler.transpile(code)
except Exception as e:
self.log.exception("Error transpiling 0background.%s" % ext)
return False
# Sandbox
sandboxer = Sandboxer(code, "0background.%s" % ext, io=self.io)
safe_code = sandboxer.toSafe()
self.log.debug("Running 0background.%s" % ext)
self.threads.append(gevent.spawn(safe_code))
def findTranspiler(self, ext):
try:
return importlib.import_module("BackgroundProcessing.transpilers.%s" % ext)
except ImportError:
try:
return importlib.import_module("transpilers.%s" % ext)
except ImportError:
return None
# Stop all threads
def stopAll(self):
for scope0 in self.io["scope0"]:
for f in scope0.to_close:
f(self.io)
for thread in self.threads:
thread.kill(block=False)
self.threads = []
self.io["scope0"] = []